Refactoring method 63: extract superclass (extract superclass)

Source: Internet
Author: User

2Classes have similar features.Create a superclass for the two classes and move the same features to the superclass.

Motivation: RepetitionCodeIs one of the worst in the system. If you do the same thing in different places, once you need to modify those actions, you have to make more changes in plain white.

Some form of repeated code is: two classes do similar things in the same way, Or do similar things in different ways. Objects provide a mechanism to simplify this situation, that is, inheritance. However, before creating these generic classes, you often cannot find such a common class. Therefore, after the common classes appear, you often start to establish the inheritance structure.

Another option is extract class (extraction class ). The choice between the two schemes is actually the choice between inheritance and delegation. If two classes can share behaviors or interfaces, inheritance is simple. If you choose the wrong one, there will always be replace inheritance with delegation (replace inheritance with Commission) this bottle of regret medicine can be taken.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.