Before model learning
What is the design model: when we design the program, we gradually formed some typical problems and solutions, which is the software model; each mode describes a problem that often occurs in our program design and the solution to the problem. When we encounter the problem described by the mode, the corresponding solution can be used to solve the problem. This is the design mode.
A design pattern is an abstract thing. It is not learned but used. Maybe you don't know any pattern at all, and you don't consider any pattern, but write the best code, even from the perspective of "pattern experts", they are all the best designs and have to say "best pattern practices". This is because you have accumulated a lot of practical experience, knowing "where to write code" is a design pattern.
Some people say: "If the level is not reached, you can learn it in vain. If the level is reached, you can do it yourself ". It is true that the mode is well-developed and may still not be able to write good code, let alone design a good framework. Oop understanding and practical experience reach a certain level, and it also means to summarize a lot of good design experience, however, it may not be the case that "no teacher can do anything", or you can say that, on the basis of level and experience, it is time for the system to learn the "mode, it is helpful to learn the experts' summary and verify your own shortcomings.
This series of design patterns learning notes is actually a Learning Record for the book "Java and patterns.
Status mode definition
State mode, allowing an object to change its behavior when its internal state changes.
State mode, which encapsulates the behavior of the object to be studied in different State objects. Each State object belongs to a subclass of an abstract state class.
The status mode and Policy mode are easy to confuse. The Strategy mode is the implementation of multiple algorithms in a business scenario, such as sales promotion-buy more discount promotions, full discount promotions; The status mode is a business scenario, encapsulation of multiple States.
How to distinguish the rule mode from the status mode:
(1) check whether the environment role has obvious excessive statuses and statuses;
(2) The environment of the Policy mode selects a specific strategy class; the environment of the state mode is put in a specific State by external reasons;
(3) Policies in the Policy mode often do not clearly tell the client the specific policy selected. The status mode is the opposite, and the status of the environment role clearly tells the client.
Status mode structure
The structure of the State mode is the same as that of the Policy mode, but the structure represents different connotations. One is based on Inheritance of multiple policies in one scenario, one is encapsulation inheritance of multiple States of a policy.
Structure chart
Roles involved
(1) Abstract State role: defines an interface to encapsulate the behavior of a specific State of an environment (context) object;
(2) concretestate role: each specific state class implements a behavior corresponding to a state in the context;
(3) Context Role: defines interfaces of interest to the client and retains an instance of a specific status class. The instance of this specific status class shows the existing status of the environment object.
Code Implementation
public class StateTest {public static void main(String[] args){Context c = new Context();c.setState(new ConcreteState());c.sampleOperation();}}class Context{private State state;public void setState(State state){this.state=state;}public void sampleOperation(){state.sampleOperation();}}interface State {void sampleOperation();}class ConcreteState implements State{@Overridepublic void sampleOperation() {System.out.println("ok");}}
The explicit Code of the Status mode is the same as that of the Policy mode.
Applicable scenarios of status Mode
(1) the behavior of an object depends on its status. As the status changes, the behavior also changes;
(2) there is a large amount of code in the conditional judgment language. At this time, you can transfer the code to a specific status class.
Some scenarios of the Status mode: Implementation of the TCP protocol and Determination of multiple statuses during user logon.
Design Mode learning notes -- state Mode