Design Patterns Visitor Pattern Notes

illustrate

Record the writing method of learning design pattern-Visitor pattern. The JDK version used is version 1.8.

Iterator(visitor)

Intent : Represents an operation that acts on each element in an object structure. It allows defining new operations for each element without changing its class.
Structure :
Insert image description here

in:

  • Visitor declares a Visit operation for each class of ConcreteElement in the object structure. The name and characteristics of the operation identify the class that sent the Visit request to the visitor, which allows the visitor to determine the specific class of the element being visited. This allows visitors to access it directly through the element's specific interface.
  • ConcreteVisitor (concrete visitor) implements each operation declared by Visitor. Each operation implements part of the algorithm, and the algorithm fragment is the class corresponding to the object in the structure. ConcreteVisitor provides context for the algorithm and stores its local state. This state often accumulates results while traversing the structure.
  • Element defines an Accept operation that takes an intermediary as a parameter.
  • ConcreteElement (concrete element) implements the Accept operation with a visitor as a parameter.
  • ObjcctStructure (object structure) can enumerate its elements and can provide a high-level interface to allow the visitor to access its elements; it can be a combination or a collection, such as a list or an unordered set.

applicability:

  • Programs whose object structures are relatively stable, but whose operating algorithms often change.
  • The objects in the object structure need to provide a variety of different and unrelated operations, and it is necessary to avoid letting changes in these operations affect the structure of the object.

Table of contents

Insert image description here

Visitor pattern example class diagram

Insert image description hereInsert image description here
Implement the visitor pattern example with this UML class diagram.

Abstract visitor role class

package com.example.deesign_patterns.visitor;

//抽象访问者角色类
public interface Person {
    
    

    //喂食宠物猫
    void feed(Cat cat);

    //喂食宠物狗
    void feed(Dog dog);
}

Abstract element role class

package com.example.deesign_patterns.visitor;

//抽象元素角色类
public interface Animal {
    
    

    //接受访问者访问的功能
    void accept(Person person);
}

pet cats

package com.example.deesign_patterns.visitor;

//宠物猫类,具体元素角色类
public class Cat implements Animal{
    
    

    @Override
    public void accept(Person person) {
    
    
        person.feed(this);//访问者给宠物猫喂食
        System.out.println("好好吃,喵喵喵。。。");
    }
}

pet dogs

package com.example.deesign_patterns.visitor;

//宠物狗类,具体元素角色类
public class Dog implements Animal{
    
    

    @Override
    public void accept(Person person) {
    
    
        person.feed(this);//访问者给宠物狗喂食
        System.out.println("好好吃,汪汪汪。。。");
    }
}

own class

package com.example.deesign_patterns.visitor;

//自己类,具体访问者角色类
public class Owner implements Person{
    
    

    @Override
    public void feed(Cat cat) {
    
    
        System.out.println("主人喂食猫");
    }

    @Override
    public void feed(Dog dog) {
    
    
        System.out.println("主人喂食狗");
    }
}

other humans

package com.example.deesign_patterns.visitor;

//其他人类,具体访问者角色类
public class Someone implements Person{
    
    

    @Override
    public void feed(Cat cat) {
    
    
        System.out.println("其他人喂食猫");
    }

    @Override
    public void feed(Dog dog) {
    
    
        System.out.println("其他人喂食狗");
    }
}

Family category

package com.example.deesign_patterns.visitor;

import java.util.ArrayList;
import java.util.List;

//家类,对象结构类
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

package com.example.deesign_patterns.visitor;

//测试类
public class Client {
    
    

    public static void main(String[] args) {
    
    
        //创建Home对象
        Home home=new Home();
        //添加元素到Home对象中
        home.add(new Cat());
        home.add(new Dog());
        //创建主人对象
        Owner owner=new Owner();
        //让主人喂食所有宠物
        home.action(owner);
        //创建其他人对象
        Someone someone=new Someone();
        //让其他人喂食所有宠物
        home.action(someone);
    }
}

Insert image description here

benefit:

  • Good scalability. Add new functionality to elements in the object structure without modifying the elements in the object structure.
  • Good reusability. Visitors are used to define functions common to the entire object structure, thereby improving the degree of reuse.
  • Separate irrelevant behaviors. Use visitors to separate irrelevant behaviors and encapsulate related behaviors together to form a visitor, so that the function of each visitor is relatively single.

shortcoming:

  • Object structure changes are difficult. In the visitor pattern, 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".
  • Violates the dependency inversion principle. The visitor pattern relies on concrete classes rather than abstract classes.

Guess you like

Origin blog.csdn.net/weixin_48040732/article/details/131365578