Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to implement the Visitor pattern
    text
    copied!<p>I'm trying to get to grips with the visitor method in Java. </p> <p>I'm trying to write a very simple program to get used to it. Basically it is a Food Menu. I want to read in user input (food type (starter, main course...) and the name of the food (pasta, fish...)) and then add this item to the menu.</p> <p>I'm fairly sure I have the code correct so far, I'm just struggling to figure out how I pass the values read in from the user.</p> <p>One of my friends who is also a programmer told me that you are supposed to have all of your functionality in the visitor classes (or at least as much as possible). </p> <p>So do I take the user input and create it into a Menu Element? Then have the visitor add the element to the Menu? (I also want to be able to remove items from the Menu but I'm assuming that this is just reverse engineering the method to add)</p> <p>Or do I not go so far as to have the visitor actually add the element. For example; would I just create the Menu Element and then pass that, then have the adding functionality in the Menu class?</p> <p>To me it would make sense to have the visitor actually add the item, as it is functionality I want to keep specific to the adding visitor, but every time I try to implement I keep getting told that I have to make the arraylist containing the Menu Elements static, and I can't help but think I am doing it wrong. </p> <p>I'm not 100% sure that the Visitor Pattern is correct for what I am trying to do?</p> <p>Personally I believe I am either really, really close..... or WAY OFF!! </p> <p>Any help you guys can offer would be great, even if you can point me towards a good tutorial that will help explain how to correctly use this pattern.</p> <p>Here is what I have so far:</p> <pre><code>interface MenuElementVisitor { void visit(Starter starter); void visit(MainCourse mainCourse); void visit(Desert desert); void visit(Drinks drinks); void visit(Menu menu); } </code></pre> <p>Menu Element Classes</p> <pre><code>interface MenuElement { void accept(MenuElementVisitor visitor); // MenuElements have to provide accept(). } class Starter implements MenuElement { private String name; public Starter(String name) { this.name = name; } public String getName() { return this.name; } public void accept(MenuElementVisitor visitor) { visitor.visit(this); } } class MainCourse implements MenuElement { private String name; public MainCourse(String name) { this.name = name; } public String getName() { return this.name; } public void accept(MenuElementVisitor visitor) { visitor.visit(this); } } class Desert implements MenuElement { private String name; public Desert(String name) { this.name = name; } public String getName() { return this.name; } public void accept(MenuElementVisitor visitor) { visitor.visit(this); } } class Drinks implements MenuElement { private String name; public Drinks(String name) { this.name = name; } public String getName() { return this.name; } public void accept(MenuElementVisitor visitor) { visitor.visit(this); } } </code></pre> <p>The Menu Class</p> <pre><code>class Menu implements MenuElement { MenuElement[] elements; public MenuElement[] getElements() { return elements.clone(); // Return a copy of the array of references. } public Menu() { this.elements = new MenuElement[] { new Starter("Soup"), new Starter("Pate"), new MainCourse("Steak"), new MainCourse("Fish"), new Desert("Ice Cream"), new Desert("Apple Tart"), new Drinks("7up"), new Drinks("Wine"), }; } public void accept(MenuElementVisitor visitor) { for(MenuElement element : this.getElements()) { element.accept(visitor); } visitor.visit(this); } } </code></pre> <p>Visitor to Add Items to the Menu</p> <pre><code>class MenuElementAddVisitor implements MenuElementVisitor { System.out.println("Press 1 for Starter, 2 for Main Course, 3 for Desert or 4 for Drinks"); int MenuElementType = Console.readInt(); System.out.println("Type the name of the Menu Element you want to add"); String MenuElementName = Console.readString(); </code></pre> <p>Visitor to Remove Items from the Menu</p> <pre><code>class MenuElementRemoveVisitor implements MenuElementVisitor { } </code></pre> <p>Run the code demo</p> <pre><code>public class VisitorDemo { static public void main(String[] args) { Menu menu = new Menu(); menu.accept(new MenuElementAddVisitor()); menu.accept(new MenuElementRemoveVisitor()); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload