当前位置:网站首页>Visitor mode

Visitor mode

2020-12-08 14:24:11 Follow your heart

Visitor mode : Represents an operation on each element of an object structure . It allows you to define new operations on elements without changing their classes .

The applicable scenario of visitor mode :

  • The object structure is relatively stable , But it is often necessary to define new operations on this object structure .
  • There are many different and unrelated operations on objects in an object structure , And you need to avoid these operations “ Pollution ” Classes for these objects , You also do not want to modify these classes when adding new operations .

Visitor pattern component :

  • Visitor( Abstract visitor role ): Declare an access operation interface for the specific element role in the object structure . The name and parameters of the operation interface identify the specific element roles that send access requests to specific visitors , In this way, visitors can access the element directly through the specific interface of its role .
  • ConcreteVisitor( Specific visitor roles ): Realization Visitor Declared interface , It gives the specific behavior of accessing each element class .
  • Element( Elemental characters ): Defines an accept access operation (accept(Visitor visitor)).
  • ConcreteElement( Specific element roles ): Implement the receiving operation interface defined by element role .
  • ObjectStructure( Structure object role ): Managed the collection of elements , And you can iterate over these elements to provide visitors with access to .

Visitor pattern component example :
Abstract Visitor :

public interface Visitor {

    void visit(ConcreteElement1 el1);
    void visit(ConcreteElement2 el2);

}

Specific visitors :

public class ConcreteVisitor implements Visitor {
    @Override
    public void visit(ConcreteElement1 el1) {
        el1.doSomething();
    }

    @Override
    public void visit(ConcreteElement2 el2) {
        el2.doSomething();
    }
}

Elements :

public interface Element {

    void accept(Visitor visitor);
    void doSomething();

}

Specific elements :

public class ConcreteElement1 implements Element {
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    @Override
    public void doSomething() {
        System.out.println("update the element1");
    }
}
public class ConcreteElement2 implements Element {
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    @Override
    public void doSomething() {
        System.out.println("update the element2");
    }
}

The structure of the object :

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

public class ObjectStructure {
    private List<Element> list = new ArrayList<>();

    public void accept(Visitor visitor) {
        list.forEach(element -> element.accept(visitor));
    }

    public void add(Element element) {
        list.add(element);
    }

    public void remove(Element element) {
        list.remove(element);
    }

}

Client calls :

public class App {
    public static void main(String[] args) {
        ObjectStructure objectStructure = new ObjectStructure();
        objectStructure.add(new ConcreteElement1());
        objectStructure.add(new ConcreteElement1());
        objectStructure.add(new ConcreteElement2());
        objectStructure.accept(new ConcreteVisitor());
    }
}

Visitor pattern summary :

  • The data structure is decoupled from the operations acting on the structure , So that the set of operations can be changed independently
  • Separation of roles and responsibilities , Comply with the single responsibility principle
  • Excellent scalability , If you need to add new visitors , Add implementation class ConcreteVisitor You can quickly expand
  • Body elements announce details to visitors , That is to say, visitors pay attention to the internal details of other classes , In violation of Dimitar's principle
  • It's difficult to change specific elements
  • Violation of the principle of Dependence Inversion , In order to achieve “ Differential treatment ” And it depends on concrete classes , There is no abstraction

版权声明
本文为[Follow your heart]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208142314173b.html