Design Pattern _ state_status Pattern

Source: Internet
Author: User

Image example:

When interacting with MM, you must pay attention to her status. In different states, her behavior may be different. For example, if you ask her to go to a movie this evening, if mm is not interested in you, he will say "something is going on". If mm is not so annoying to you but doesn't like it, he will say "okay, but can I bring my colleagues? ", If you like your mm, you will say, "What time is it? How about watching a movie ?", Of course, if you are doing well during a movie, you can also turn the status of mm from dislike to liking.

Status mode:

The State mode allows an object to change its behavior when its internal state changes. This object seems to have changed its class. The state pattern encapsulates the behavior of the studied object in different State objects. Each State object belongs to a subclass of an abstract state class. The intention of the state mode is to change the behavior of an object when its internal state changes. The State mode creates a subclass of the state class for the possible states of each system. When the system status changes, the system changes the selected subclass.

Purpose:
Allows an object to change its behavior when its internal state changes.

UML structure diagram:


Resolution:
The State mode mainly solves the problem of different processing operations that are often encountered during development according to different States, most people use the switch-case statement for processing, which will cause a problem: too many branches, and if a new State is added, the original code needs to be compiled. the State mode encapsulates these different States to handle such problems. When the state changes, the state is processed and then switched to another State, that is to say, the status switching responsibility is handed over to the specific status class for responsibility. at the same time, the state mode and the Strategy Mode have many similarities in the figure. It should be noted that the two ideas are consistent, but the encapsulated things are different: the State mode encapsulates different states, while the stategy mode encapsulates different algorithms.

Design Pattern _ state_status Pattern

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.