Document your initial understanding of the visitor pattern.

Introduction to the

  • Represents an operation that operates on elements in an object structure.
  • It allows us to define new operations on elements without changing their classes.

Simply break it down:

  • You have an object structure, and you have a lot of classes in that structure, and you have two element classes in that structure, Men and Woman.
  • Define a new operation without changing the element class. For example, Men and Woman have operations before: fall in love and get married. Then came the operation: dating. Without changing the element class, the client can implement it. Share data operations from object structures.

Concrete implementation see preliminary use.

Initial use

Requirements:

  • There is an object structure that holds men and women. We need to show their status in love and marriage.
  • Men fall in love, output I have a girlfriend. Women fall in love, output I have a boyfriend.
  • When a man gets married, he says I have a wife. When a woman gets married, she says I have a husband.

This procedure is discussed in two steps.

  • Actualization state: the state in which every man and woman falls in love and gets married.
  • Implement object structure: Create a class that holds both male and female objects and displays their states.

Implementation status:

First, realize the various states corresponding to men and women.

To achieve 1:

  • Defines a protocol Person with the methods love and marry.
  • Create two classes Man and Woman, each implementing its own methods, following the protocol Person.
// func love {print("I have a girlfriend."} // a woman is in love.print("I have a boyfriend."} // when a man marriesprint("I have a wife."} // marry {print("I have a husband.")}Copy the code

However, if there is a new operation – date. So, the structure above, we need

  • Add another method appointment to the Person protocol.
  • Add the respective implementations in Man and Woman.

This violates the open and closed principle, so how can you add a new operation without changing an existing class?

Realization of 2:

  • Pull the operations out and make them classes.
    • There’s a Visitor protocol for Person, Visitor, that accesses all of the people.
    • Action classes follow this protocol and implement corresponding methods.
  • Change the methods in the Person protocol to those accessible to visitors.
    • Accept: Parameter is an operation object that follows the Visitor protocol.
    • Each implementation calls the VISIT operation in the Visitor. Inside Man, call visitMan, Woman call visitWoman. And pass Self. So that visitors like Love can operate Person more conveniently, such as obtaining the name, age and other attributes of the corresponding Person.

Implements the object structure

The following code

Struct PersonList {var pList = [Person]() func display(vistor: Visitor) {for person inPList {person.accept(visitor: vistor)}}let man1 = Man()
let woman1 = Woman()
let man2 = Man()
letWoman2 = woman1 var pList = PersonList() plist.plist.append (man1) plist.plist.append (woman1) Plist.plist.append (woman2) plist.plist.append (woman2) //3let love = Love()
letDisplay (visitor: love) display(visitor: love)Copy the code

At this point, add the date operation to the object in the object structure.

  • Create the Appointment class that follows the Visitor protocol and implement the corresponding methods to access the object.
  • When called externally, add the following code
let appointment = Appointment()
pList.display(visitor: appointment)
Copy the code

Again using

A basic understanding of the visitor pattern can be gained from the above example, and a requirement for employee statistics can also be applied to this pattern.

  • The staff consists of regular and temporary employees
  • The human resources department counts their hours, and the finance department calculates wages. Refer to the link

In other cases, I’ll summarize and fill in later.

conclusion

From the example above, you can also see the advantages and disadvantages of this pattern. For example, if there is another type of expatriate on the staff, there is more than one type of men and women on the staff. Then the program is also very troublesome to change.