Design Mode-State)

Source: Internet
Author: User


The following is the implementation code of this class chart:
Package com. plabmedia. state;
Public interface LightState {
/**
* Print the current status
*/
Public void print (Light light );
}
Package com. plabmedia. state;
Public class RedLight implements LightState {
@ Override
Public void print (Light light ){
System. out. println ("current state is red ");
Light. setState (new GreenLight ());
}
}
Package com. plabmedia. state;
Public class YellowLight implements LightState {
@ Override
Public void print (Light light ){
System. out. println ("current state is yellow ");
Light. setState (new RedLight ());
}
}
Package com. plabmedia. state;
Public class GreenLight implements LightState {
@ Override
Public void print (Light light ){
System. out. println ("current state is green ");
Light. setState (new YellowLight ());
}
}
Package com. plabmedia. state;
Public class Light {
Private LightState state;
Public LightState getState (){
Return state;
}
Public void setState (LightState state ){
This. state = state;
}
Public void print (){
State. print (this );
}
Public Light (LightState state ){
This. state = state;
}
}
Package com. plabmedia. state;
Public class Client {
Public static void main (String args []) {
Light light = new Light (new GreenLight ());
Light. print ();
Light. print ();
Light. print ();
Light. print ();
Light. print ();
Light. print ();
}
}
Running result:
Current state is green
Current state is yellow
Current state is red
Current state is green
Current state is yellow
Current state is red
The above is a small implementation of the state mode. This implementation is only cognitive. In our practical application, if we use the state mode, we should be more responsible than this, the basic structure is similar. We will discuss the status mode below.
1. When to use the status mode:
The behavior of an object depends on its state. The behavior of an object must change with its State;
When we need to evolve conditional transfer statements, we can encapsulate each branch into a state class. When writing code, I do not like conditional transfer statements, in particular, the best else statement, I think it bears too many things, and it will be used to handle any situations that are not satisfied before. Why.
2. Notes about the status mode:
Who will define the status change: the client or the status is determined internally? This depends on the application environment at that time. If the state changes, I think it can be set on the client. If the state changes, it depends on the previous state, for example, in the workflow system, the current status depends on the previous status.
Status object Creation Time: when required? Or are all objects created in advance for calling as needed? This must be determined by the subsystem. If a system has a fixed sequence of state changes and is not so frequent, you can create the required objects as needed. Otherwise, in terms of performance, you must first create various State objects for calling when needed.
You can upload the environment class to a specific status class, And the status class calls the environment class as needed. For example, you can directly call the print method of the Environment class in each status class, in this case, the Client only needs to initialize the environment class and then you can see that its status is constantly changing.
3. Differences between the status mode and the policy mode:
I mean, based on my understanding, the difference between the two lies in that the State mode is in collective combat, while the Strategy Mode is individual combat? The status mode has different behaviors because of its internal status changes. For the policy mode, as long as the client selects a policy, this policy applies throughout the lifecycle. Therefore, when distinguishing between the State mode and the policy mode, it mainly depends on whether the system has any state changes. If the system has only one State throughout the lifecycle, it is good, select the policy mode. If the system has different States throughout the process, select the state mode.
Let's write so much about it. I hope everyone will take the time to discuss it and talk about their views on the status mode.
Author: bangzhu

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.