State mode of C + + design mode (iv)

Source: Internet
Author: User

4. Summary of State mode

The state pattern encapsulates the different behavior of an object in different states in a State class, allowing the environment object to behave differently by setting different state objects. While the details of the state transitions are transparent to the client, the client does not directly manipulate the State class, and it does not need to know the state transition details, which reduces the coupling between the client and the specific State class. State classes and environment classes are a combination of relationships that when a client invokes a method of an environment class, the Environment class delegates the method of calling the State class. The use of State mode encapsulates a specific state class to avoid if...else congestion, making code easier to maintain and more extensible. At the same time encapsulation of the State class, but also embodies the "single principle." In the actual development, the state mode has a high frequency of use.

1. Key Benefits

The main advantages of the state mode are as follows:

(1) encapsulated the state of the transformation rules , in the state mode can be the state of the transformation code encapsulated in the Environment class or the specific state class, the state transformation code can be centralized management, rather than scattered in a business method, in line with the "single principle."

(2) put all behaviors related to a state into a class , and only inject a different state object to make the environment object behave differently.

(3) allowing the state transition logic to be integrated with the state object rather than providing a huge conditional statement block , state mode allows us to avoid using large conditional statements to weave business methods and state transformation code together.

(4) You can have multiple environment objects share a state object , thereby reducing the number of objects in the system.

2. Main Disadvantages

The main disadvantages of the state mode are as follows:

(1) The use of State mode will inevitably increase the number of classes and objects in the system, resulting in increased system operating overhead .

(2) The structure and implementation of State mode are more complex, if improper use will result in the confusion of program structure and code, and increase the difficulty of system design .

(3) State mode support for "open and close principle" is not very good , adding new state classes need to modify those responsible for state transformation of the source code, or can not be converted to new state, and modify the behavior of a State class also need to modify the corresponding class source code.

3. Applicable scenarios

You can consider using the state mode in the following situations:

(1) The behavior of an object depends on its state (such as some property values), and changes in state will result in changes in behavior.

(2) There are a number of conditional statements in the code that are related to the state of the object, which can result in poor maintainability and flexibility of the code, inability to easily add and remove states, and result in a coupling enhancement between the customer class and the class library.

4. State mode specific application

(1) Elevator lifting system design: There is open, close, run, stop state. There will be different behaviors in each state. For example, a stop operation can be performed in a running state, but cannot be opened or closed.


(2) Voting system design: Voting 1 times is the normal voting status, the number of votes between 2---5 times is the repeat voting status, the number of votes between 5---8 times is a malicious voting state, the number of votes more than 8 times the blacklist status.

(3) Hotel Reservation system design: The existence of reservation status, check-in status, cancellation status, check-out status. Each State should have different behavior.

(4) Supermarkets, hotels, KTV There are different levels of users, the level of users in different states, the behavior should have different permissions; In the game also has a variety of different role states, each State should have different behavior.

     

(5) Bank withdrawal system design: There is a normal state, overdraft status, frozen state. There will be different behaviors in different states. For example: The frozen state cannot make withdrawals, while the normal state and overdraft status can be withdrawn.

(6) Operating system task scheduling status diagram: There is a waiting state, a ready state, a running state, a stop state.

(7) There are three handshake states in the TCP network connection process. Send connection requests, answer requests, establish connections, disconnect, and so on.


(8) transaction processing mechanism in the database. There are Oldclean states, Olddirty states, Olddelete states, deleted states.

(9) In the field of industrial control, there are a large number of state diagrams in the field of communication, some chips also have time series diagram, high and low level changes, system clock signal changes, there are a large number of changes in state.

(10) Patterns of state in life: from children to middle age, to old age, is the change of life state, from 35 degrees to 15 degrees is the climate change; self-made to wealthy is a change of career.

Mode of State for C + + design mode (iv)

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.