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
Encapsulates operations that operate on elements of a data structure. It can define new operations that operate on those elements without changing the data structure.
The main separation of data structures from data operations.
The advantages and disadvantages
Advantages: 1, in line with the principle of single responsibility. 2. Excellent scalability. 3. Flexibility.
Disadvantages: 1. Specific elements disclose details to visitors, which violates the Demeter principle. 2. It is difficult to change specific elements. 3. It violates the principle of dependency inversion by relying on concrete classes instead of abstraction.
Usage scenarios
1. The corresponding class of an object in an object structure rarely changes, but it is often necessary to define new operations on this object structure. 2. You need to do many different and unrelated operations on objects in an object structure, and you want to avoid “contaminating” the classes of those objects with these operations, and you don’t want to modify those classes when you add new operations.
implementation
We can understand it through some examples in life, such as a guest in the house, the guest is a visitor, he can do some things, but not all things; Or to the Internet cafe Internet Xiaoming, Xiaoming is also a visitor, he can play games in the Internet cafe, watch videos, listen to music and so on, but can not destroy the equipment in the Internet cafe and so on. With this understanding, we can get a sense of what the visitor pattern is mainly about.
The Visitor pattern consists of five roles: abstract Visitor, ConcreteVisitor, abstract Node, ConcreteNode, and ObjectStructure.
- Visitor role: Declares one or more method operations that form the interface that all concrete Visitor roles must implement.
- The ConcreteVisitor role: Implements the interface declared by the abstract visitor, that is, the individual access operations declared by the abstract visitor.
- Abstract Node role: Declares an accept operation that takes a visitor object as a parameter.
- ConcreteNode Roles: Implement accept operations specified by abstract nodes.
- ObjectStructure role: Has the following responsibilities, can traverse all elements in the structure.
Let’s use a simple example to illustrate. The library has a computer, but can only play games and look at pictures. Tom and Tom used the computer, so they could be visitors. Define an abstract visitor with a way to play games and view images. An abstract node computer is then defined to accept the request.
Abstract Visitor role
// Abstract the Visitor role
interface Visitor {
// You can access the game
void visit(Games games);
// You can access images
void visit(Photos photos);
}
Copy the code
Abstract Node roles
// Abstract Node role: Declares an accept operation that takes a visitor object as a parameter.
interface Computer {
void accept(Visitor visitor);
}
Copy the code
ConcreteNode Role
// ConcreteNode roles: Implement the accept operations specified by abstract nodes.
class Games implements Computer {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String play(a) {
return "play Games"; }}Copy the code
// ConcreteNode roles: Implement the accept operations specified by abstract nodes.
class Photos implements Computer {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String watch(a) {
return "watch photos"; }}Copy the code
ObjectStructure role
// ObjectStructure role: has the following responsibilities, can traverse all elements in the structure.
class ObjectStructure {
private List<Computer> computers = new ArrayList<Computer>();
public void action(Visitor visitor) {
for(Computer c : computers) { c.accept(visitor); }}public void add(Computer computer) { computers.add(computer); }}Copy the code
The verification results
class Test {
public static void main(String[] args) {
// Create a structure object
ObjectStructure os = new ObjectStructure();
// Add a node to the structure
os.add(new Games());
// Add a node to the structure
os.add(new Photos());
// Create 2 visitors
Visitor visitorZhangSan = new PersonVisitor("zhangsan");
Visitor visitorLisi = new PersonVisitor("lisi"); os.action(visitorZhangSan); os.action(visitorLisi); }}Copy the code
zhangsan-----play Games
zhangsan-----watch photos
lisi-----play Games
lisi-----watch photos
Copy the code