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）
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
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 .
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 Patternstructure ：Command Abstract command classConcreteCommand Specific command class ： Hold executor referenceInvoker caller / Requestor ： Holds a reference to a specific command classReceiver The receiver / practitionersClient
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 dataiterator ： Traversal dataRealization ： Use inner class definitions , You can directly access the properties of the external classJDK 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 receivePULL （pull）As long as the observer knows the situation , As for when to get content , What to get , You can make your own decisions
Target audience ： Observers gather , Method ： change 、 noticeSpecific targets ： Identify the status valueAbstract observer ： Behavior after receiving notificationConcrete 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
Abstract mediatorAbstract colleaguesSpecific intermediary ： Holding the aggregation of specific colleagues , And can deliver informationSpecific colleagues ： Holding references from specific intermediaries , Can interact
The essence ：
Decouple the interaction between objects
Application in development ：
MVC Medium Cjava.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 .
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 patternUsed to describe how to construct a simple language interpreter , It is mainly used for compiler and interpreter design using object-oriented languageTry 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 .
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
Context The environment class
An State object , This object defines the current state
State Abstract state classConcreteState Specific status class
Encapsulates the behavior of the 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
Interface definition algorithm , Specific implementation of different algorithms to form algorithm clustersThe caller has interface references , The client dynamically specifies
GUI Layout management in programmingSpring Medium Resouce Interface , Resource access policyjavax.servlet.http.HttpServlet#service()
Responsibility chain pattern （ The chain of responsibility model chain of responsibility）
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
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
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 .
Represents an operation on each element of an object structure , It allows us to define new operations on elements without changing their classes
XML Document parser design and other complex collection object processing