Github source address
An overview of 23 design patterns
- Java Language Design – An overview of 23 design patterns
Creation pattern
- Factory Method Pattern
- Abstract Factory Pattern
- Builder Mode
- Prototype mode
- Singleton
Structural mode
- Facade Pattern
- Adapter mode (Adapter)
- Proxy mode
- Composite mode
- Flyweight Mode
- Decorator pattern
- Bridge mode (Bridge)
Behavioral pattern
- Mediator Mode
- Observer Model
- Command mode
- Iterator pattern (Iterator)
- Template Method
- Strategy Pattern
- State mode
- Memento Mode
- Interpreter mode
- Chain of Responsibility model
- Visitor Pattern
define
Allows an object to change its behavior when its internal state changes, and the object appears to modify its class.
The advantages and disadvantages
Advantages: 1. Encapsulate conversion rules. Enumerate possible states. Before enumerating states, determine the state type. 3. Put all the behavior related to a state into a single class, and you can easily add new states by simply changing the object’s state to change its behavior. 4. Allow state transition logic to be integrated with state objects, rather than some huge block of conditional statements. 5. Multiple environment objects can share a state object, thus reducing the number of objects in the system.
Disadvantages: 1. The use of state mode will inevitably increase the number of system classes and objects. 2. The structure and implementation of state mode are complicated, which will lead to confusion of program structure and code if improperly used. 3. State modes do not support the “on/off principle” very well. For state modes that can be switched, adding a new state class requires modifying the source code responsible for the state transition, otherwise the new state cannot be switched.
Usage scenarios
1. Scenarios where behavior changes with state change. 2. Substitute for conditional and branch statements.
So let’s get to that before we get to thatGeneral state judgment
andSwitching state
“General state judgment” uses if.. Else if(condition type is multiple and complex, you can choose policy mode), judge condition has no relation with state, code is as follows:
if (witch == 'A') {
state = 'B';
} else if (witch == 'B') {
state = 'C';
} else if (witch == 'C') {
state = 'A';
}
Copy the code
“Toggle state” is if “witch” in parentheses becomes “state”
if (state == 'A') {
state = 'B';
} else if (state == 'B') {
state = 'C';
} else if (state == 'C') {
state = 'A';
}
Copy the code
Change the state from “A” to “B” and then to “C”; After switching to “A”, like A toggle switch, this State change can be used in State mode.
The state pattern is to write the state of each if statement as a subclass, replacing a state with a subclass that contains the behavior of the state and the transitions between the states.
implementation
-
Context: The environment class has objects in various states and acts as an interface for external use, responsible for calling the state-class interface.
-
State: An abstract State can be either an abstract class or directly defined as an interface. It is used to define state abstraction methods, the implementation of which is the responsibility of subclasses.
-
ConcreteState: ConcreteState classes are implementors of abstract states. Different state classes correspond to different states, and their internal implementations are different. Different processing logic can be implemented when objects in different states are used in environment classes
A TV set has two states, off state and on state. The shutdown state can only be switched on, and the boot state can be switched on and off.
Abstract state class
public interface State {
/** * boot */
void onState(a);
/**
* 关机
*/
void offState(a);
/** ** */
void previousState(a);
/** ** next channel */
void nextState(a);
}
Copy the code
The television class
public class TV {
// Start state
private State state;
// Shutdown status
private State offState;
// Start state
private State onState;
public TV(a){
offState = new OffState(this);
onState = new OnState(this);
// The initial state is shutdown
state = offState;
}
/ / off the phone
public void offState(a){
state.offState();
}
// Next channel
public void next(a){
state.nextState();
}
// Go to the next channel
public void previousState(a){
state.previousState();
}
/ / boot
public void onState(a){
state.onState();
}
public State getState(a) {
return state;
}
public void setState(State state) {
this.state = state;
}
public State getOffState(a) {
return offState;
}
public void setOffState(State offState) {
this.offState = offState;
}
public State getOnState(a) {
return onState;
}
public void setOnState(State onState) {
this.onState = onState; }}Copy the code
Shutdown status class
public class OffState implements State {
TV tv;
public OffState(TV tv){
this.tv = tv;
}
@Override
public void onState(a) {
System.out.println("TV on: Welcome ~~~");
// The status changes to startup
tv.setState(tv.getOnState());
}
@Override
public void offState(a) {
/ / no operation
}
@Override
public void previousState(a) {
/ / no operation
}
@Override
public void nextState(a) {
/ / no operation}}Copy the code
Startup status class
public class OnState implements State{
TV tv;
public OnState(TV tv){
this.tv = tv;
}
@Override
public void onState(a) {
/ / no operation
}
@Override
public void offState(a) {
System.out.println("TV shutdown: Welcome to ~~~~~ next time.");
tv.setState(tv.getOnState());
}
@Override
public void previousState(a) {
System.out.println("Go to the last channel at ------");
tv.setState(tv.getOnState());
}
@Override
public void nextState(a) {
System.out.println("Next channel at ------"); tv.setState(tv.getOnState()); }}Copy the code
validation
class Test {
public static void main(String[] args) {
TV tv = new TV();
/ / boot
tv.onState();
// Next channel
tv.next();
// Go to the next channel
tv.previousState();
/ / off the phonetv.offState(); }}Copy the code
TV boot: welcome ~ ~ ~ -- -- -- -- -- - the next channel into the -- -- -- -- -- - a channel on the TV to turn it off: watch ~ ~ ~ ~ ~ welcome next timeCopy the code
Contrast policy patterns with state patterns
- In policy mode, the function of a class is to actively change based on current conditions;
- In state mode, the function of a class is to passively change from the current state;
- There is no correlation between each behavior or algorithm in the policy pattern;
- The states in the state mode are related, and the states themselves control the state transition.