Iterator mode, visitor mode

Iterator mode

Definition: Provide an object to sequentially access a series of data in the aggregated object without exposing the internal representation of the object during aggregation.

Pros and cons

advantage:

  1. Access the content of an aggregate object without exposing its internal representation ;
  2. The traversal task is completed by the iterator, which simplifies the aggregation class ;
  3. It supports traversing an aggregation in different ways , and can even customize the subclass of iterator to support new traversals;
  4. It is very convenient to add new aggregation classes and iterator classes , without modifying the original code;
  5. Good encapsulation , providing a unified interface for traversing different aggregation structures.

Disadvantages: The number of classes is increased, which increases the complexity of the system to a certain extent.

Application scenarios

  1. When it is necessary to provide multiple traversal methods for aggregated objects .
  2. When it is necessary to provide a unified interface for traversing different aggregation structures .
  3. When accessing the content of an aggregated object without exposing the representation of its internal details .

main character

  1. Abstract aggregation (Aggregate) role: defines the interface for storing, adding, and deleting aggregate objects, and creating iterator objects.
  2. Concrete Aggregate (ConcreteAggregate) role: to implement an abstract aggregation class, and return an instance of an iterator.
  3. Abstract Iterator role: defines the interface for accessing and traversing aggregate elements, usually including the following methods: hasNext(), first(), next(), etc.
  4. The role of ConcreteIterator: implements the methods defined in the abstract iterator interface, completes the traversal of the aggregate object, and records the current position of the traversal.

Structure chart

Insert picture description here

Applications

There are many scenic spots and historical sites in Wuyuan, so we should design a program to view the pictures and brief introductions of the scenic spots. The structure diagram is as follows:
Insert picture description here

Expansion of the pattern

The iterator pattern is often used in combination with the composition pattern. When accessing the container components in the composition pattern, the iterator is often hidden in the container construction class of the composition pattern. Of course, you can also construct an external iterator to access container components. The structure diagram is as follows:
Insert picture description here

Visitor mode

Definition: Separate the operations that act on each element in a certain data structure and encapsulate them into independent classes , so that new operations that act on these elements can be added without changing the data structure. This is for each element in the data structure. Each element provides multiple access methods .
It separates the operation of the data from the data structure, and it is the most complicated pattern in the behavioral pattern.

Pros and cons

advantage:

  1. Good scalability . Ability to add new functions to the elements in the object structure without modifying the elements in the object structure ;
  2. ** Good reusability**. The general functions of the entire object structure can be defined by the visitor, thereby increasing the degree of reuse of the system;
  3. Good flexibility . The visitor model decouples the data structure from the operations acting on the structure, so that the set of operations can evolve relatively freely without affecting the data structure of the system;
  4. Comply with the single responsibility principle . The visitor pattern encapsulates related behaviors to form a visitor, so that each visitor has a single function.

Disadvantages:

  1. It is difficult to add new elements . In the visitor mode, every time a new element class is added, a corresponding specific operation must be added to each specific visitor class, which violates the "opening and closing principle" ;
  2. Destroy the package . The specific elements in the visitor model announce the details to the visitor, which destroys the encapsulation of the object;
  3. Violated the principle of dependency inversion . The visitor pattern relies on concrete classes instead of abstract classes.

Application scenarios

  1. A program whose object structure is relatively stable, but its operating algorithm often changes .
  2. Objects in the object structure need to provide a variety of different and unrelated operations , and it is necessary to avoid allowing changes in these operations to affect the structure of the object .
  3. The object structure contains many types of objects, and it is hoped that some operations that depend on their specific types can be implemented on these objects .

main character

  1. Abstract visitor (Visitor) class: defines an interface for accessing specific elements, and corresponds to a visit operation visit() for each specific element. The parameter type in this operation identifies the specific element to be visited.
  2. Specific visitor (ConcreteVisitor) role: implements each access operation declared in the abstract visitor role, and determines what the visitor should do when accessing an element.
  3. Abstract element (Element) role: Declare an interface containing the accept() receiving operation, and the received visitor object as the parameter of the accept() method.
  4. Concrete element (ConcreteElement) role: implements the accept() operation provided by the abstract element role. The method body is usually visitor.visit(this). In addition, the concrete element may contain operations related to its own business logic.
  5. Object structure (ObjectStructure) role: is a container containing element roles, providing a method for visitors to traverse all elements in the container, usually implemented by aggregate classes such as List, Set, and Map.

Structure chart

Insert picture description here

Applications

Art companies use "copper" to design bronze statues and "paper" to draw pictures; coinage companies use "copper" to design copper coins and "paper" to print banknotes;
for the two elements of "copper" and "paper", two Each company handles it differently. The structure diagram is as follows:
Insert picture description here

Note: The contents of the article picking in the "software design patterns (Java version)", Author: Cheng fine column

Guess you like

Origin blog.csdn.net/weixin_44048668/article/details/110731454