Java design mode-state mode

Source: Internet
Author: User

The Marquis Yi Chime, unearthed in Hubei in 1979, can produce different tones for every clock.
In the object-oriented language, the chime has properties that can be dynamically changed, that is, the sound it can emit. This property of the chime is called the state, and the chime is called a stateful object. The sound the bells can make is determined by the bell that beats, and the number of clocks is determined in advance by the state of each clock.
In many cases, the behavior of an object depends on one or more dynamically changing properties, such as a state, which is called a stateful object.
The class diagram for the state mode is as follows:

The role of State mode design is:
Abstract State role: defines an interface that encapsulates the behavior of a particular state of an environment object
Specific status roles (Concretestate): Each specific state class implements the behavior that corresponds to a state of the environment.
Environment Role (context): defines the interface to which the client is interested, and preserves an instance of a specific State class.
As can be seen, the environment class context is the class being examined, and his behavior sampleoperation () is delegated to an object of type state. Since the state itself is an abstract class or interface, the actual status class that actually assumes this task is: Concretestate. Only a specific state class is given, and there are many specific state classes in the actual situation. In other words, the environment class context behavior sampleoperation () is delegated to a specific State class.
By using the polymorphism principle, the content of the state of the environment class context can be changed dynamically, from the point of pointing to a specific State class to another specific status class, so that the behavior of the Environment Class sampleoperation () is executed by different concrete states.
Source of environmental role:

 Public  class Context {    PrivateState State; Public void sampleoperation() {state.sampleoperation (); } Public void setState(State State) { This. state = State; }} Abstract state interface: Public  Interface  State {     Public void sampleoperation();} Specific status class: Public  class concretestate implements  State {     Public void sampleoperation() {//Do something}}


when to use the state mode
1. The behavior of an object depends on the state in which it is located, and the object's behavior must change as its state changes
2, the object in a certain method depends on a heavy or multiple conditional transfer statements, which have a large number of code. The state mode wraps each branch of the conditional transfer statement into a separate class. Enables these conditional transfer branches to exist and evolve in a class-independent way. Maintaining these independent classes will no longer affect other parts of the system.

Java design mode-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.