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:
- Access the content of an aggregate object without exposing its internal representation ;
- The traversal task is completed by the iterator, which simplifies the aggregation class ;
- It supports traversing an aggregation in different ways , and can even customize the subclass of iterator to support new traversals;
- It is very convenient to add new aggregation classes and iterator classes , without modifying the original code;
- 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
- When it is necessary to provide multiple traversal methods for aggregated objects .
- When it is necessary to provide a unified interface for traversing different aggregation structures .
- When accessing the content of an aggregated object without exposing the representation of its internal details .
main character
- Abstract aggregation (Aggregate) role: defines the interface for storing, adding, and deleting aggregate objects, and creating iterator objects.
- Concrete Aggregate (ConcreteAggregate) role: to implement an abstract aggregation class, and return an instance of an iterator.
- Abstract Iterator role: defines the interface for accessing and traversing aggregate elements, usually including the following methods: hasNext(), first(), next(), etc.
- 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
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:
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:
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:
- Good scalability . Ability to add new functions to the elements in the object structure without modifying the elements in the object structure ;
- ** 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;
- 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;
- 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:
- 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" ;
- Destroy the package . The specific elements in the visitor model announce the details to the visitor, which destroys the encapsulation of the object;
- Violated the principle of dependency inversion . The visitor pattern relies on concrete classes instead of abstract classes.
Application scenarios
- A program whose object structure is relatively stable, but its operating algorithm often changes .
- 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 .
- 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
- 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.
- 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.
- Abstract element (Element) role: Declare an interface containing the accept() receiving operation, and the received visitor object as the parameter of the accept() method.
- 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.
- 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
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: