Reconstruction Method 31: replace type code with subclass (replace type code with subclass)

Source: Internet
Author: User

 

You have an immutable type code that affects the behavior of the class.Replace this type code with a subclass.

Motivation: If the type code you are facing does not affect the behavior of the host class, you can use replace type code with class (replace type code with class) to process them. However, if the type code affects the behavior of the host class, the final method is to use polymorphism to process the changed behavior.

In general, the flag of this situation is a conditional expression like switch. This conditional expression may have two forms: switch statement or if-then-else structure. In either form, they check the type code value and perform different actions based on different values. In this case, you should refactor with replace conditional with polymorphism (replace conditional expressions with polymorphism. But for that smooth reconstruction, we should first Replace the type code with an inheritance system with multi-state behavior. Such an inheritance system should take the type code host class as the base class and create a subclass for each type code.

But in the following two cases, you cannot do this. 1) the type code value changes after the object is created. 2) for some reason, the type code host class already has a subclass. If you are facing either of these two situations, you need to use replace type code with statestrategy (replace type code with State policy ).

Replace type code with subclass(Replace type codes with subclasses) is mainly used to build a stage, allowing replace conditional with polymorphism (replace conditional expressions with polymorphism) to show its skills. If no conditional expression exists in the host class, replace type code with class (replace type code with class) is more appropriate and has lower risks.

Another reason for using replace type code with subclass (replace type code with subclass) is that the host class has the "only related to objects with specific types of code" feature. After the reconstruction of this item, you can use the push down method (function down) and push down field (field down) to push these features to the appropriate subclass, to demonstrate the fact that they are only relevant to specific situations.

Replace type code with subclass(Replace the type code with a subclass) the advantage is that it transfers "understanding of different behaviors" from class users to the class itself. To add new behavior changes, you only need to add a subclass. If there is no polymorphism mechanism, you must find all the conditional expressions and modify them one by one. Therefore, This refactoring will be especially valuable if it is possible to add new behaviors.

 

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.