Soft test: Strategy vs Status

Source: Internet
Author: User

Policy mode:      

The policy pattern encapsulates different algorithm families, allowing them to replace each other, ensuring that the algorithm changes independently of the customer using the algorithm. From the duck instance, we can see how the strategy encapsulates the change, how the composition is used, and how it is programmed for the interface.

First edition:

          Each duck subclass inherits the superclass, As a result, all ducks can swim and do the same. If some ducks fly, you can only add fly () So it's not perfect to use inheritance to achieve reuse. What if a rubber duck? will only squeak, or a wooden duck will not bark or fly? This is someone who thought of using a rubber duck's squeak to cover the Super class, and someone put fly () and quack () abstract from the superclass and put in flyable interface (only the fly will implement this interface) and quackable interface (only the squeaky implementation of the interface).

Second Edition:


because not all ducks fly and croak, inheritance is not the proper way, although abstract interfaces can solve the problem (no flying rubber ducks), but there will be a lot of duplicate code, then the strategy appears.

Third edition:

          fly () and quack () duck flybehavior So the actual implementation will not be tied to death in the duck subclass (Previous practice: The behavior comes from the duck superclass specific implementation, or inherit an interface that is implemented by subclasses, both of which depend on" Implementation ", are implemented tightly tied to the dead, no way to change behavior, Unless you write more code).


        Specific implementation:Duckadded inFlybehaviorand theQuackbehaviorTwo instance variables, run-time hold specific behavior references, duck subclasses instantiate specific behavior, such as the Green Duck's behavior is entrusted toQuackobject, if the client wants a green-headed duck that will fly, instantiate an object first, then call directlyDuckmethod, because the subclass inheritsDuck, and a specific behavior has been instantiated in the subclass, so the client can get whatever effect it wants by combining it.

State mode:

It helps the object to control its behavior by changing the state inside the object, and the object looks as if it has modified its class.


When a candy machine is designed, it is packaged for behavior, such as inserting coins, returning coins, turning the crank and releasing candies, and then judging what they might appear to be, but if a new state is added, then each class is modified.

          Then there's a new design: Wrap the state object together, First define a state interface, each action of the candy machine corresponds to a method, and then for the candy machine each state is encapsulated into a class, adding a reference to the candy machine as the parameter of the method in the class, implementing state interface. Place all state and instance variables in the Candy machine class state Each state also creates a state instance, and the implementation of those behaviors is delegated to the current state. The specific state handles the request from the candy machine, which provides the implementation for the request, so the behavior changes when the candy machine changes state. This localized each state behavior into its own class, and also removed the if ... else statement, let each state to modify the closure, let the candy machine to expand open.

Strategy VS Status:

Because the two design pattern class diagrams are very similar, they only focus on differences. In a strategy, the customer usually actively specifies the policy object to be combined by the Context, and usually only one of the most appropriate, such as the Green Duck is a quack, fly with wings, so you can change the behavior by combining objects. The state is a group of behaviors encapsulated in the state, you can Simply modify the state object through the context to change its behavior,thecontext of the behavior can be delegated to a state at any time, However, the customer knows little about state objects.

Soft test: Strategy vs Status

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.