Last active
July 30, 2017 13:12
-
-
Save dstanek/b067381eaca6a13ae5b11db304e419ef to your computer and use it in GitHub Desktop.
Some visitor examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# An example visitor where the visitor doesn't know the internal | |
# structure of the object heirarchy. | |
class Department: | |
def __init__(self, name, employees): | |
self._name = name | |
self._employees = employees | |
def accept(self, visitor): | |
visitor.visit_department(self._name) | |
for employee in self._employees: | |
employee.accept(visitor) | |
class Employee: | |
def __init__(self, name, email): | |
self._name = name | |
self._email = email | |
def accept(self, visitor): | |
visitor.visit_employee(self._name, self._email) | |
class Visitor: | |
def visit_department(self, name): | |
print('Department: {}'.format(name)) | |
def visit_employee(self, name, email): | |
print(' Employee: {} <{}>'.format(name, email)) | |
if __name__ == '__main__': | |
dept = Department('IT', [ | |
Employee('Adam', '[email protected]'), | |
Employee('Bill', '[email protected]'), | |
Employee('Cenk', '[email protected]'), | |
]) | |
dept.accept(Visitor()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# An example visitor using type checking | |
class Department: | |
def __init__(self, name, employees): | |
self.name = name | |
self.employees = employees | |
def accept(self, visitor): | |
visitor.visit(self) | |
for employee in self.employees: | |
employee.accept(visitor) | |
class Employee: | |
def __init__(self, name, email): | |
self.name = name | |
self.email = email | |
def accept(self, visitor): | |
visitor.visit(self) | |
class Visitor: | |
def visit(self, thing): | |
if isinstance(thing, Department): | |
print('Department: {}'.format(thing.name)) | |
elif isinstance(thing, Employee): | |
print(' Employee: {} <{}>'.format(thing.name, thing.email)) | |
if __name__ == '__main__': | |
dept = Department('IT', [ | |
Employee('Adam', '[email protected]'), | |
Employee('Bill', '[email protected]'), | |
Employee('Cenk', '[email protected]'), | |
]) | |
dept.accept(Visitor()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# An example visitor without using type checking | |
class Department: | |
def __init__(self, name, employees): | |
self.name = name | |
self.employees = employees | |
def accept(self, visitor): | |
visitor.visit_department(self) | |
for employee in self.employees: | |
employee.accept(visitor) | |
class Employee: | |
def __init__(self, name, email): | |
self.name = name | |
self.email = email | |
def accept(self, visitor): | |
visitor.visit_employee(self) | |
class Visitor: | |
def visit_department(self, dept): | |
print('Department: {}'.format(dept.name)) | |
def visit_employee(self, employee): | |
print(' Employee: {} <{}>'.format(employee.name, employee.email)) | |
if __name__ == '__main__': | |
dept = Department('IT', [ | |
Employee('Adam', '[email protected]'), | |
Employee('Bill', '[email protected]'), | |
Employee('Cenk', '[email protected]'), | |
]) | |
dept.accept(Visitor()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment