Java 23 design patterns-17. State mode of behavioral mode

In addition to the design principles in Java, there are also 23 design patterns.

These patterns have been accumulated by the predecessors bit by bit, have been improving, and have been optimizing, and these design patterns can solve some specific problems.

And in these modes, it can be said that the use of language is fully reflected.

Then we are going to learn   the state mode in the   behavioral mode today     !

State mode

Let’s take a look at what is called state mode

We can learn from the encyclopedia that the state mode allows an object to change its internal state, and then change its behavior, so that the object seems to modify its class, which may be more abstract. Let’s take an example.

As far as the weather is concerned, the weather can be sunny, rainy, windy, etc. Different weather will have different behaviors. For example, it is sunny, it is impossible to have squally showers, but it should be cloudless. The sun shines

Definition and characteristics of state mode

The definition of State mode: For stateful objects, complex "judgment logic" is extracted into different state objects, allowing state objects to change their behavior when their internal state changes.

State mode is an object behavioral mode, and its main advantages are as follows.

1. The state mode localizes the behaviors related to a specific state into one state, and separates the behaviors of different states to meet the "single responsibility principle".

2. Reduce the interdependence between objects. Introducing different states into independent objects will make state transitions more explicit and reduce the interdependence between objects.

3. Conducive to the expansion of the program. It is easy to add new states and transitions by defining new subclasses.


The main disadvantages of state mode are as follows.

1. The use of state mode will inevitably increase the number of classes and objects in the system.

2. The structure and implementation of the state mode are relatively complicated, and if used improperly, it will cause confusion in the program structure and code.

The structure and realization of the state pattern

The state mode packs the behavior of objects that are changed by the environment into different state objects, and its intention is to make an object change its behavior when its internal state changes.

The state model includes the following main roles:

1. Environment role: Also called context, it defines the interface that customers are interested in, maintains a current state, and delegates state-related operations to the current state object for processing.

2. Abstract state role: define an interface to encapsulate the behavior corresponding to a specific state in the environment object.

3. Specific state role: realize the behavior corresponding to the abstract state.

 

After knowing this, let's implement the above example

first step:

Let's first define the abstract state class, this class is the weather

After we have defined it, we will add a method for this weather

The second step:

Then let's define our concrete realization abstract state class

third step:

Then we need an environmental role, we can check the current weather by changing the state

package com.java.state;

public class Context {

	private Weather weather;
	private Integer status;
	
	public Context(Integer status) {
		this.status = status;
		if(this.status == 1) {
			this.weather = new SunnyDay();
		}
		if(this.status == 2) {
			this.weather = new WindDay();
		}
		if(this.status == 3) {
			this.weather = new SnowDay();
		}
	}

	public Weather getWeather() {
		return weather;
	}

	public void setWeather(Weather weather) {
		this.weather = weather;
	}
	
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
		if(this.status == 1) {
			this.weather = new SunnyDay();
		}
		if(this.status == 2) {
			this.weather = new WindDay();
		}
		if(this.status == 3) {
			this.weather = new SnowDay();
		}
	}

	public void weather() {
		this.weather.weather();
	}
}

When we instantiate the environment, we can pass an Integer type variable, and let this variable bind the status in this class, so that by modifying the status, create the corresponding type according to the variable, and call the specific implementation

test:

Let's get a Test class to test

Let's take a look

Then we modify the status

Isn’t it OK, everyone feel it

 

OK, that's it, everyone take a good look. Practice a lot. If you have any questions, please contact me QQ: 2100363119

Welcome everyone to visit my personal website: lemon1234.com Thank you for leaving a message

Guess you like

Origin blog.csdn.net/weixin_45908370/article/details/109492527