Design Patterns Series Articles
Design Patterns (1): Creational Singleton Patterns
Design Patterns (2, 3): Creational Factory Methods and Abstract Factory Patterns
Design Patterns (4): Creational Prototype Patterns
Design Pattern (5): Creational Builder Pattern
Design Pattern (6): Structural Agent Pattern
Design Pattern (7): Structural Adapter Pattern
Design Pattern (8): Structural Decorator Pattern
Design Pattern (9): Structural Bridge Pattern
Design Patterns (10): Structural Appearance Patterns
Design Patterns (11): Structural Combination Patterns
Design Pattern (12): Structural Flyweight Pattern
Design Pattern (13): Behavioral Template Method Pattern
Design Patterns (14): Behavioral Strategy Patterns
Design Patterns (15): Behavioral Command Patterns
Design Pattern (16): Behavioral Chain of Responsibility Pattern
Design Patterns (17): Behavioral State Patterns
Design Pattern (18): Behavioral Observer Pattern
Design Patterns (19): Behavioral Intermediary Patterns
Design Pattern (20): Behavioral Iterator Pattern
Design Pattern (21): Behavioral Visitor Pattern
Table of contents
1. Classification of Design Patterns
- Creational patterns
- Used to describe "how to create objects", its main feature is "separation of object creation and use"
- Provides singletons, prototypes, factory methods, abstract factories, and builders
5 种创建型模式
- structural pattern
- Used to describe how to organize classes or objects into larger structures in a certain layout
- Proxy, Adapter, Bridge, Decorator, Facade, Flyweight, Composition are provided
7 种结构型模式
- behavioral model
- Used to describe how classes or objects cooperate with each other to complete tasks that a single object cannot do alone, and how to assign responsibilities
- Provides template methods, policies, commands, chain of responsibility, state, observers, mediators, iterators, visitors, mementos, interpreters
11 种行为型模式
2. Visitor mode
1 Overview
definition
- Encapsulates operations that operate on elements of a data structure
- It is possible to define new operations on these elements without changing the data structure
2. Structure
The Visitor pattern contains the following main roles:
(Element)
Abstract visitor (Visitor) role: defines the behavior of visiting each element , its parameters are elements that can be accessed, and its number of methods is theoretically the same as the number of element classes (the number of Element implementation classes) Yes, it is not difficult to see from this point that the visitor pattern requires that the number of element classes cannot be changed- Specific visitor (ConcreteVisitor) role: gives specific behaviors when visiting each element class
- Abstract element (Element) role: defines a method to accept visitors (
accept
), which means that each element must be accessible by visitors - Specific element (ConcreteElement) role: Provide a specific implementation of the access method, and this specific implementation usually uses the method provided by the visitor to access the element class
- Object Structure (Object Structure) role: the object structure mentioned in the definition, the object structure is an abstract expression, the specific point can be understood as a class with container properties or compound object characteristics, it will contain a set of elements ( ),
Element
and These elements can be iterated for access by the visitor
3. Realize
【Example】Feed a pet
- Pets are also divided into dogs, cats, etc., if you want to feed pets
- The owner can feed, and so can others
- Visitor role: person who feeds pets
- Specific Visitor Roles: Owner, Others
- Abstract element role: animal abstract class
- Specific element roles: pet dog, pet cat
- Structural Object Role: Host
The class diagram is as follows:
code show as below:
- Create an abstract visitor interface
public interface Person {
//喂食宠物狗
void feed(Cat cat);
//喂食宠物猫
void feed(Dog dog);
}
- To create different concrete visitor roles (master and others), all need to implement
Person
the interface
public class Owner implements Person {
@Override
public void feed(Cat cat) {
System.out.println("主人喂食猫");
}
@Override
public void feed(Dog dog) {
System.out.println("主人喂食狗");
}
}
public class Someone implements Person {
@Override
public void feed(Cat cat) {
System.out.println("其他人喂食猫");
}
@Override
public void feed(Dog dog) {
System.out.println("其他人喂食狗");
}
}
- Define abstract node - pet
public interface Animal {
//接受访问者访问的功能
void accept(Person person);
}
- Define
Animal
concrete nodes (elements) that implement the interface
public class Dog implements Animal {
@Override
public void accept(Person person) {
//访问者给宠物狗喂食
person.feed(this);
System.out.println("好好吃,汪汪汪!!!");
}
}
public class Cat implements Animal {
@Override
public void accept(Person person) {
//访问者给宠物猫喂食
person.feed(this);
System.out.println("好好吃,喵喵喵!!!");
}
}
- Defines the object structure, in this case the master's home
public class Home {
//声明一个集合对象,用来存储元素对象
private List<Animal> nodeList = new ArrayList<Animal>();
//添加元素功能
public void add(Animal animal) {
nodeList.add(animal);
}
public void action(Person person) {
//遍历集合,获取每一个元素,让访问者访问每一个元素
for (Animal animal : nodeList) {
animal.accept(person);
}
}
}
- test class
public class Client {
public static void main(String[] args) {
Home home = new Home();
home.add(new Dog());
home.add(new Cat());
Owner owner = new Owner();
home.action(owner);
Someone someone = new Someone();
home.action(someone);
}
}
4. Advantages and disadvantages
advantage
- Good scalability, adding new functions to elements in the object structure without modifying the elements in the object structure
- Good reusability, through the visitor to define the common functions of the entire object structure, thereby improving the degree of reusability
- Separate irrelevant behaviors, separate irrelevant behaviors through visitors, encapsulate related behaviors together to form a visitor, so that the function of each visitor is relatively single
shortcoming
- It is very difficult to change the object structure. In the visitor mode, every time a new element class is added, corresponding specific operations must be added to each specific visitor class, which violates the "opening and closing principle".
- In violation of the dependency inversion principle, the visitor pattern relies on concrete classes instead of abstract classes
5. Usage scenarios
- A program whose object structure is relatively stable, but its operation algorithm changes frequently
- The objects in the object structure need to provide many different and unrelated operations, and the changes of these operations should not affect the structure of the object