I used the Visitor design pattern to solve one of the issues within our system. As a reference how to implement it I used DoFactory site and This YouTube video.
In DoFactory example, the Visitor uses a method with return type of "void" and in YouTube video, author uses "double".
Why I ask:
After presenting the solution to company CTO, he accepted to call it Visitor, but he claims like if Visitor is not "void" as stated in GoF, than it like abuse the real Visitor pattern.
Question:
Is it required for Visitor pattern to return "void"?
I mean in order to be "real Visitor pattern" as DoFactory (GoF) describes it, or it can be any return type and can still be called "real Visitor pattern"?
Visitor design pattern is one of the behavioral design patterns. It is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.
It is one way to follow the open/closed principle. In essence, the visitor allows adding new virtual functions to a family of classes, without modifying the classes.
The Visitor pattern represents an operation to be performed on the elements of an object structure without changing the classes on which it operates. This pattern can be observed in the operation of a taxi company. When a person calls a taxi company (accepting a visitor), the company dispatches a cab to the customer.
Visitor is a behavioral design pattern that allows adding new behaviors to existing class hierarchy without altering any existing code.
Design Patterns are meant to be used as a guide to show how to solve common computer science problems. You are welcome to deviate from the "real" implementation any way you wish.
As for your example youtube video, the author of that shows a how you can use the visitor pattern to compute taxes for different types of items. Each visit
method returned a double for the amount of money including taxes each item would cost. Different Visitor implementations were then made to show how you could have different ways to compute taxes (normal vs tax holiday etc) without changing your code.
This example was a "toy" problem and was meant to teach how the visitor pattern worked in an easy to understand way- and it does a good job.
While I say you are welcome to deviate from the GoF implementation, it might not be a good idea to mimic the code from this video.
There were a few things in the video that would be a bad idea to use in a real program. For example using double
for money. I think the return double (for money) was just a way to quickly show how the visitor worked and you probably shouldn't use it.
If you wanted to modify the code in the video to return void instead. The easiest solution would be to have a private field in the TaxVisitor
that accumulates the total value and increment it inside each visit method. Then have a getter to get the final total.
The author also explicitly invokes each food item in his visitor example which doesn't show off the power of the Visitor Pattern. I would have had a container object of the grocery items that would be visitable and its accept method would visit each item in the receipt.
GroceryList groceries = new GroceryList();
groceries.add(new Milk(...));
groceries.add(new Liquor(...));
...
TaxVisitor visitor = new TaxVisitor();
visitor.accept(groceries);
Money tax = visitor.getTax();
Money preTaxTotal = groceries.getTotalPreTax();
Money total = preTaxTotal.plus(tax);
//or compute tax during tax holiday
TaxVisitor holidayVisitor = new TaxHolidayVisitor();
holidayVisitor.accept(groceries);
Money holidayTax = holidayVisitor.getTax();
Money holidayTotal = preTaxTotal.plus(holidayTax);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With