Behavioral patterns

 

 

 

Focus on the interaction between objects in the system , This paper studies the communication and cooperation between objects at runtime , Further clarify the responsibilities of the target .

 

 

 

 

 

Template method pattern (template method)

 

 

 

 

Introduce :

 

 

The template method defines the algorithm skeleton in an operation , There will be some steps ( Hook method ) Delay to subclass implementation . such , The new subclass can redefine some specific steps of an algorithm without changing its structure

The core :
The code that handles a process already has , But the code of one of the nodes can't be determined for the time being . therefore , We adopt the factory approach model , Transfer the code implementation of this node to the subclass to complete . namely : The parent class of the processing step is defined , The implementation is deferred to the subclass definition

Method callback
In the development of soft armour , A child class cannot call a parent class , And call the subclass through the parent class . These call steps have been defined in the parent class , The whole process is completely controlled by the parent class .

application :
spring JDBCTemplate

 

 

Command mode (command)

 

 

 

 

Introduce :
Encapsulate a request as an object , This allows us to parameterize customers with different requests ; Queue or log requests , And support for undo operations . Also known as : action Action Pattern 、 Business transaction Pattern
structure :
Command Abstract command class
ConcreteCommand Specific command class : Hold executor reference
Invoker caller / Requestor : Holds a reference to a specific command class
Receiver The receiver / practitioners
Client

application :

Struts2 in Action, Revocation and reinstatement of orders , The underlying implementation of transaction mechanism

 

 

 

 

Iterator pattern (iterator) Also known as cursor mode (curson)

 

 

 

 

scene :
Provides a way to traverse aggregate objects .
role :
Aggregate object : Store the data
iterator : Traversal data
 
Realization : Use inner class definitions , You can directly access the properties of the external class
 
JDK Built in Iterator(List/Set)

 

 

 


Observer mode (Observer)

 

 

 

The core of the broadcast mechanism

 

 

 

 

The core :

 

 

The observer model is mainly used for 1:N The notice of . Be an object ( Target audience —— The publisher of the news Subject) When the state changes , He needs to tell a series of people in time ( Observer object ,Observer), Make them respond .
 
How to inform the observer
 
PUSH (push)
Every time the notification is broadcast to all observers , All observers can only passively receive
PULL (pull)
As long as the observer knows the situation , As for when to get content , What to get , You can make your own decisions
structure :
PUSH
Target audience : Observers gather , Method : change 、 notice
Specific targets : Identify the status value
Abstract observer : Behavior after receiving notification
Concrete observer : Identify the status value
 
JDK Provide java.util.Observable and java.util.Observer To implement the observer pattern

 

Intermediary model (Mediator)

 

The core :
If the connection between objects in a system is presented as a mesh , There are a lot of many to many relationships between objects , Will lead to a relationship and its complexity , Call these objects “ Colleagues ”.
We can introduce an intermediary object , Make each colleague object interact with the mediator object only , To dissolve the complex network structure into a star structure

structure

Abstract mediator
Abstract colleagues
Specific intermediary : Holding the aggregation of specific colleagues , And can deliver information
Specific colleagues : Holding references from specific intermediaries , Can interact
 
The essence :
Decouple the interaction between objects
Application in development :
MVC Medium C
java.lang.reflect.Method#invoke()

 

 

 

 

Memo mode (memento)
 

 

 

 

 

The core :
Is to save a copy of the internal state of an object , In this way, the object can be restored to its original state later .
structure :
Source generator class Originator
Responsible for creating memos , It is used to record the internal state of the current time itself , And you can use the memo to restore the internal state
Memorandum class Memento
Responsible for the storage Originator The internal state of an object , And can prevent Originator Access to objects other than
Responsible for human CareTaker
Be responsible for the preservation of Memento

 

 

Interpreter mode (Interpreter)

 

 

 

Introduce :
An unusual design pattern
Used to describe how to construct a simple language interpreter , It is mainly used for compiler and interpreter design using object-oriented language
Try not to use interpreter mode , It's not easy to maintain later . In the project , have access to Jruby/Groovy/java Of JS Engine instead of parser , make up java Deficiency .

scene :

EL expression 、 Regular expressions 、sql grammar 、 Mathematical expression (Math Expression String Parser 、Expression4J)

 

 

The state pattern (State)
 

 

 

The core :
It is used to solve the state transformation of complex objects in the system and the encapsulation of behaviors in different states
 
structure :
Context The environment class
An State object , This object defines the current state
State Abstract state class
ConcreteState Specific status class
Encapsulates the behavior of the state
application :
java Thread.State

 

 

 

 

The strategy pattern (Strategy)
 

 

 

 

 

The strategy pattern corresponds to a cluster of algorithms to solve a problem , The user is allowed to select any algorithm from the cluster for processing , At the same time, it is convenient to change the algorithm or add new algorithm . And it's up to the client to call that algorithm .

 

The essence :
Separation algorithm 、 Selective implementation
Realization :
Interface definition algorithm , Specific implementation of different algorithms to form algorithm clusters
The caller has interface references , The client dynamically specifies
application :
GUI Layout management in programming
Spring Medium Resouce Interface , Resource access policy
javax.servlet.http.HttpServlet#service()

 

 

 

 

Responsibility chain pattern ( The chain of responsibility model chain of responsibility)
 

 

 

 

 

Definition
Link objects that can handle requests of the same type into a chain , The submitted request passes along the chain , The objects in the chain decide one by one whether they have the ability to process the request , Deal with if you can , If not, it is passed to the next object
 
Realization :
Linked list mode :
Define an abstract class , Class contains the next object reference in the linked list . The abstraction of concrete logical methods is realized by inheritance subclasses .
Non linked list mode :
Through set 、 Data generation responsibility chain
 
application :
java Exception capture in 、js In the event of bubbling / Capture 、Servelt in filter

 

 

Visitor mode (Visitor)

 

 

Pattern motivation :
For objects stored in a collection , They may have different types ( Even if the same interface is implemented ), For objects in this collection , You can accept an object called a visitor to access , Different visitors have different ways of visiting .
Definition
Represents an operation on each element of an object structure , It allows us to define new operations on elements without changing their classes
application :
XML Document parser design and other complex collection object processing