Design mode 10 state Mode 1

Source: Internet
Author: User

State mode, which allows an object to change its behavior when the internal state changes, and the object looks as if it had modified its class.

This pattern encapsulates the state into a separate class and delegates the action to an object representing the current state, and we know that the behavior changes with the internal state. The candy machine provides a good example: when the candy machine is in the noquarterstate or hasquarterstate two different states, you put 25 cents, you will get different behavior (the machine accepts 25 cents and the machine rejects 25 cents).

If the object you are using can completely change its behavior, you will feel that the object is actually instantiated from another class. However, in fact, we are using a combination to create the illusion of class changes by simply referencing different state objects.

The model of analogy and strategy. In state mode, we encapsulate a group of behaviors in a state object, and the context's behavior can be delegated to one of those state objects at any time. As the actual passage progresses, the current state is changed in the set of State objects to reflect the internal state of the context, and thus the context's behavior is changed. But the context of the customer for the state objects do not know much, or even simply unaware.

In the case of policy mode, the customer usually proactively specifies which of the policy objects the context is to combine. Now, while the policy model gives us the flexibility to change the policy at run time, there is usually only one most appropriate policy object for a context object. For example, in the first chapter, some ducks (such as green-headed ducks) are set to fly using the carrier's flying behavior, while some ducks use flying behavior only to keep them on the ground.

In general, we think of the strategy model as a flexible alternative to inheritance. If you use inheritance to define the behavior of a class, you will be trapped by this behavior, and even to modify it is difficult. With the strategy mode, you can change the behavior by combining different objects.

We think of the state pattern as an alternative to not placing many conditional judgments in context. By wrapping the behavior into a state object, you can change the behavior of the context by simply changing the state object within the context.

Design mode 10 state Mode 1

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.