Reconstruction Method 32: replace type code with State/Strategy (replace type code with State/Policy)

Source: Internet
Author: User

 

You have a type code that affects the behavior of the class, but you cannot provide inheritance methods to eliminate it.Replace the type code with a status object.

Motivation: This reconstruction is similar to replace type code with subclass (replace type code with subclass, however, if the "type code changes in the object's lifecycle" or "The host class cannot be inherited for other reasons", you can also use this refactoring. This reconstruction uses the state mode and the Strategy Mode.

StateThe pattern is very similar to the Strategy pattern, so no matter which one you choose, the refactoring process is the same. "Which mode to select" is not the key to the problem. You only need to select a mode that is more suitable for a specific situation. If you want to simplify the process with replace conditional with polymorphism (replace conditional expressions with polymorphism) after This refactoringAlgorithmSelect the Strategy Mode. If you want to move the data related to the status and consider the new object as a change state, you should select the state mode.

 

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.