From 958fea5cf0652c2cfcb0d22e11e8765ae1510159 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Tue, 13 Oct 2020 12:52:53 +0200 Subject: [PATCH] add documentation --- README.md | 1 + behavioral/README.md | 1 + behavioral/visitor/README.md | 49 ++++++++++++++++++++++++++++++++++ behavioral/visitor/__init__.py | 3 +++ 4 files changed, 54 insertions(+) create mode 100644 behavioral/visitor/README.md create mode 100644 behavioral/visitor/__init__.py diff --git a/README.md b/README.md index 7f32f59..be0677b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ and divided into three groups. - [Observer](behavioral/observer/README.md) - [State](behavioral/state/README.md) - [Strategy](behavioral/strategy/README.md) +- [Visitor](behavioral/visitor/README.md) ## Resources diff --git a/behavioral/README.md b/behavioral/README.md index e5aa8a0..8810dd6 100644 --- a/behavioral/README.md +++ b/behavioral/README.md @@ -12,3 +12,4 @@ Behavioral design patterns are concerned with algorithms and the assignment of r - [Observer](observer/README.md) - [State](state/README.md) - [Strategy](strategy/README.md) +- [Visitor](visitor/README.md) diff --git a/behavioral/visitor/README.md b/behavioral/visitor/README.md new file mode 100644 index 0000000..ec5310b --- /dev/null +++ b/behavioral/visitor/README.md @@ -0,0 +1,49 @@ +# Visitor + +Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code. + +## Problem + +Let an app which works with geographic information structured as one colossal graph. Each node of the graph may + represent a complex entity such as a city, but also more granular things like industries, sightseeing areas, _etc._. The nodes are connected with others if there’s a road between the real objects that they represent. Under the hood, each node type is represented by its own class, while each specific node is an object. + + At some point, you got a task to implement exporting the graph into XML format. At first, the job seemed pretty straightforward. You planned to add an export method to each node class and then leverage recursion to go over each node of the graph, executing the export method. The solution was simple and elegant: thanks to polymorphism, you weren't coupling the code which called the export method to concrete classes of nodes. + +Unfortunately, the system architect refused to allow you to alter existing node classes. He said that the code was already in production and he didnt want to risk breaking it because of a potential bug in your changes. + +Besides, he questioned whether it makes sense to have the XML export code within the node classes. The primary job of these classes was to work with geodata. The XML export behavior would look alien there. + +There was another reason for the refusal. It was highly likely that after this feature was implemented, someone from the marketing department would ask you to provide the ability to export into a different format, or request some other weird stuff. This would force you to change those precious and fragile classes again. + +## Solution + +The Visitor pattern suggests that you place the new behavior into a separate class called `visitor`, instead of trying to integrate it into existing classes. The original object that had to perform the behavior is now passed to one of the visitor’s methods as an argument, providing the method access to all necessary data contained within the object. + +Now, what if that behavior can be executed over objects of different classes? For example, in our case with XML + export, the actual implementation will probably be a little bit different across various node classes. Thus, the visitor class may define not one, but a set of methods, each of which could take arguments of different types. + + But how exactly would we call these methods, especially when dealing with the whole graph? These methods have different signatures, so we can’t use polymorphism. To pick a proper visitor method that’s able to process a given object, we’d need to check its class. Doesn't this sound like a nightmare? + +You might ask, why don’t we use method overloading? That’s when you give all methods the same name, even if they support different sets of parameters. Unfortunately, even assuming that our programming language supports it at all (as Java and C# do), it won’t help us. Since the exact class of a node object is unknown in advance, the overloading mechanism won’t be able to determine the correct method to execute. It’ll default to the method that takes an object of the base Node class. + +However, the Visitor pattern addresses this problem. It uses a technique called Double Dispatch, which helps to execute the proper method on an object without cumbersome conditionals. Instead of letting the client select a proper version of the method to call, how about we delegate this choice to objects we’re passing to the visitor as an argument? Since the objects know their own classes, they’ll be able to pick a proper method on the visitor less awkwardly. They “accept” a visitor and tell it what visiting method should be executed. + +I confess. We had to change the node classes after all. But at least the change is trivial and it lets us add further behaviors without altering the code once again. + +Now, if we extract a common interface for all visitors, all existing nodes can work with any visitor you introduce into the app. If you find yourself introducing a new behavior related to nodes, all you have to do is implement a new visitor class. + +## How to Implement + +1. Declare the visitor interface with a set of “visiting” methods, one per each concrete element class that exists in the program. + +1. Declare the element interface. If you’re working with an existing element class hierarchy, add the abstract “acceptance” method to the base class of the hierarchy. This method should accept a visitor object as an argument. + +1. Implement the acceptance methods in all concrete element classes. These methods must simply redirect the call to a visiting method on the incoming visitor object which matches the class of the current element. + +1. The element classes should only work with visitors via the visitor interface. Visitors, however, must be aware of all concrete element classes, referenced as parameter types of the visiting methods. + +1. For each behavior that can’t be implemented inside the element hierarchy, create a new concrete visitor class and implement all of the visiting methods. + + You might encounter a situation where the visitor will need access to some private members of the element class. In this case, you can either make these fields or methods public, violating the element’s encapsulation, or nest the visitor class in the element class. The latter is only possible if you’re lucky to work with a programming language that supports nested classes. + +1. The client must create visitor objects and pass them into elements via “acceptance” methods. \ No newline at end of file diff --git a/behavioral/visitor/__init__.py b/behavioral/visitor/__init__.py new file mode 100644 index 0000000..8edaf42 --- /dev/null +++ b/behavioral/visitor/__init__.py @@ -0,0 +1,3 @@ +""" +Lets you separate algorithms from the objects on which they operate. +"""