当前位置:网站首页>Status mode (4.4-4.10)
Status mode (4.4-4.10)
2022-04-22 05:18:00 【Look, water is water】
In state mode (State Pattern) in , The behavior of a class changes based on its state . This type of design pattern belongs to behavioral pattern .
In state mode , We create objects that represent various states and a behavior that changes as the state object changes context object .
Introduce
Intention : Allows an object to change its behavior when its internal state changes , Object appears to modify its class .
Main solution : The behavior of an object depends on its state ( attribute ), And it can change its behavior according to its state .
When to use : The code contains a large number of conditional statements related to the state of the object .
How to solve : Abstract all kinds of concrete state classes .
Key code : Usually there is only one method in the interface of command mode . There are one or more methods in the state mode interface . and , The implementation of the state pattern class method , General return value , Or change the value of the instance variable . in other words , State mode is generally related to the state of an object . Methods that implement classes have different functions , Override the methods in the interface . The state mode is the same as the command mode , It can also be used to eliminate if…else Wait for the conditional choice statement .
Application example :1、 When playing basketball, the players can have a normal state 、 Abnormal state and abnormal state . 2、 In the chime of marquis Zeng Yi ,‘ A clock is an abstract interface ’,' The clock A’ Wait is the concrete state ,' The chime of marquis Zeng Yi ’ Is the specific environment (Context).
advantage :1、 Encapsulates the transformation rules . 2、 Enumerate possible states , You need to determine the state type before enumerating the States . 3、 Put all the behaviors related to a state into a class , And it's easy to add new states , Just changing the state of the object can change the behavior of the object . 4、 Allows state transformation logic to be integrated with state objects , Instead of a huge conditional block . 5、 You can have multiple environment objects share a state object , So as to reduce the number of objects in the system .
shortcoming : 1、 The use of state patterns will inevitably increase the number of system classes and objects . 2、 The structure and implementation of state patterns are complex , Improper use will lead to confusion of program structure and code . 3、 State mode pair " Opening and closing principle " The support is not very good , For a state mode that can switch states , Adding a new state class requires modifying the source code responsible for state transformation , Otherwise, you cannot switch to the new state , In addition, to modify the behavior of a state class, you need to modify the source code of the corresponding class .
Use scenarios :1、 A scene in which behavior changes as states change . 2、 Conditions 、 Substitute of branch statement .
matters needing attention : Use state mode when behavior is constrained by state , And the status does not exceed 5 individual .
Realization
We're going to create one State Interface and implementation State The entity state class of the interface .Context It's a class with a certain state .
StatePatternDemo, Our demo class uses Context And state objects Context A change in behavior as a state changes .
step 1
Create an interface .
State.java
public interface State {
void doAction(Context context);
}
step 2
Create entity classes that implement interfaces .
StartState.java
public class StartState implements State {
@Override
public void doAction(Context context) {
System.out.println("Player is in start state");
context.setState(this);
}
@Override
public String toString() {
return "Start State";
}
}
StopState.java
public class StopState implements State {
@Override
public void doAction(Context context) {
System.out.println("Player is in stop state");
context.setState(this);
}
@Override
public String toString(){
return "Stop State";
}
}
step 3
establish Context class .
Context.java
public class Context {
private State state;
public Context() {
state = null;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
}
step 4
Use Context To check the current status State Change in behavior when changing .
StatePatternDemo.java
public class StatePatternDemo {
public static void main(String[] args) {
Context context = new Context();
StartState startState = new StartState();
startState.doAction(context);
System.out.println(context.getState().toString());
System.out.println();
StopState stopState = new StopState();
stopState.doAction(context);
System.out.println(context.getState().toString());
}
}
step 5
Verify output .
Player is in start state
Start State
Player is in stop state
Stop State
版权声明
本文为[Look, water is water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210626121827.html
边栏推荐
- Database (II) addition, deletion, modification and query of MySQL table (basic)
- The code example of writing data into disk file is as follows:
- MySQL double master and double slave + atlas data test
- Junit Introduction et Introduction
- Batch resolves the IP address of the domain name and opens the web page
- Punch in: 4.20 C language chapter - (1) first acquaintance with C language - (10) keywords typedef, static
- Summary of browser cross domain problems
- Libevent implements UDP communication
- One way to disable Google cross domain
- [Reading Notes - > statistics] 07-01 introduction to the concept of discrete probability distribution geometric distribution
猜你喜欢

Programme de démarrage Spark: WordCount

Junit Introduction et Introduction

The eleventh job of MySQL database - Application of view

MySQL double master and double slave + atlas data test

Temperature control via mqc582tt + PNET

Prometheus basic knowledge brain map

Summary of browser cross domain problems

How to restrict Oracle sub query

The chain of implicit trust: an analysis of the web third party resources loading

Visitor mode from 2022-1-3 to 2022-1-16
随机推荐
Meetup 02期回顾:Q&A 集锦
Detailed explanation of Neptune w800 lighting (interruption) project
Reduce the graduation time to before the age of 20, and go to primary school for five years at the age of 5, so as to increase the population
Servlet lifecycle
[boutique] using dynamic agent to realize unified transaction management
Nexus私服——(二) 3.2.0版 控制台安装,初始密码位置
Programme de démarrage Spark: WordCount
QBoxSet、QBoxPlotSeries
Introduction to swagger UI
What are the challenges of Internet of things testing, and how do software testing institutions ensure quality
Star Trek - Discovery - Season 3 final episode
Go语言爬虫基础
Final Cut Pro mosaics in multiple places of the video at the same time
JUnit common notes
Temperature control via mqc582tt + PNET
2022-4-20 operation
Parsing of thread safe classes: (2.8-3.6)
Defining "disinformation"
Measuring the global recursive DNS infrastructure: a view from the edge
Enum enumeration type