
protocol Element {
func accept(_ v: CarVisitor)
}
//ConcreteElement
class Engine: Element {
func accept(_ v: CarVisitor) {
v.visit(self)
}
}
//ConcreteElement
class Wheel: Element {
private(set) var number: Int
init(_ number: Int) {
self.number = number
}
func accept(_ v: CarVisitor) {
v.visit(self)
}
}
//ConcreteElement
class Car: Element {
let items: [Element] = [
Engine(),
Wheel(1), Wheel(2),
Wheel(3), Wheel(4)]
func accept(_ v: CarVisitor) {
for e in items {
e.accept(v)
}
v.visit(self)
}
}
//Visitor
protocol CarVisitor {
func visit(_ engine: Engine)
func visit(_ wheel: Wheel)
func visit(_ car: Car)
}
//ConcreteVisitor
class TestCarVisitor: CarVisitor {
func visit(_ engine: Engine) {
print("test engine")
}
func visit(_ wheel: Wheel) {
print("test wheel #\(wheel.number)")
}
func visit(_ car: Car) {
print("test car")
}
}
//ConcreteVisitor
class RepairCarVisitor: CarVisitor {
func visit(_ engine: Engine) {
print("repair engine")
}
func visit(_ wheel: Wheel) {
print("repair wheel #\(wheel.number)")
}
func visit(_ car: Car) {
print("repair car")
}
}
//Client
let car = Car()
let v1 = TestCarVisitor()
let v2 = RepairCarVisitor()
car.accept(v1)
car.accept(v2)
Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.Read moreThe problem. A document is made of paragraphs, images and tables. The program must export it, count its words and check its spelling. If each operation is added as a method to every kind of part, the part classes grow with every new operation. The code of one operation is also spread over many classes. The idea. Each operation becomes a separate object, called a visitor. A visitor has one method for each kind of element. Each element has one method that takes a visitor and calls the visitor's method for its own kind. So the right code runs for each pair of element and operation, without checks of the type. To add a new operation, you write a new visitor; the element classes do not change. How to read the example. The elements are of several kinds. Each concrete visitor is one operation, with a separate method for each kind of element. The client code gives a visitor to the elements, and each element sends it to the method for its own kind. In most examples one element holds the others and gives the visitor to each of its parts. Two visitors then pass over the same parts, each with its own operation. In some languages the elements are plain data with a type tag, and a visitor is one function with a version for each kind. When to use it. Use Visitor when the set of element classes is stable, but new operations on them appear often. Do not use it when new kinds of element are added often: each new kind needs a new method in every visitor. A common mistake. Checking the type of the element with conditions inside one general visitor method. A new kind of element is then missed without any warning. Give each kind its own method. |