mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
26 lines
969 B
Python
26 lines
969 B
Python
from typing import List
|
|
|
|
from behavioral.visitor.component import Component, ConcreteComponentA, ConcreteComponentB
|
|
from behavioral.visitor.visitor import Visitor, ConcreteVisitor1, ConcreteVisitor2
|
|
|
|
|
|
def client_code(components: List[Component], visitor: Visitor):
|
|
"""
|
|
The client code can run visitor operations over any set of elements without
|
|
figuring out their concrete classes. The accept operation directs a call to
|
|
the appropriate operation in the visitor object.
|
|
"""
|
|
|
|
for component in components:
|
|
component.accept(visitor)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
components = [ConcreteComponentA(), ConcreteComponentB()]
|
|
print("The client code works with all visitors via the base Visitor interface:")
|
|
visitor1 = ConcreteVisitor1()
|
|
client_code(components, visitor1)
|
|
|
|
print("It allows the same client code to work with different types of visitors:")
|
|
visitor2 = ConcreteVisitor2()
|
|
client_code(components, visitor2)
|