Refactoring method 62: extract subclass (refining subclass)

Source: Internet
Author: User

Some features in the class are only used by some instances.Create a new subclass and move the features mentioned above to the subclass.

Motivation: the main motivation for using extract subclass is: You find that some behaviors in the class are only used by some instances, and other instances do not need them. Sometimes the difference in this behavior is identified by type codes. In this case, you can use replace type code with subclass (replace type code with subclass) or replace type code with State/Strategy (replace type code with State policy ). However, it is not necessary to have a type code to indicate that subclass needs to be considered.

Extract class(Refining class) is another choice outside of extract subclass (refining sub-class). The choice between two is actually the choice between delegation and inheritance. Extract subclass (extract subclass) is usually easier, but it also has restrictions: once an object is created, you cannot change the type-related behavior, however, if you use extract class, you only need to insert another component to change the behavior of the object. In addition, subclass can only represent a group of changes. If you want a class to change in several different ways, you must use the delegate.

 

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.