Skip to content

Instantly share code, notes, and snippets.

@rocketpages
Created September 25, 2012 22:40
Show Gist options
  • Select an option

  • Save rocketpages/3784907 to your computer and use it in GitHub Desktop.

Select an option

Save rocketpages/3784907 to your computer and use it in GitHub Desktop.
An example of heterogeneous collections using the Visitor pattern
public class App
{
public static void main(String[] args)
{
// The main collection
Collection<Visitable> visitables = new ArrayList<Visitable>();
visitables.add(new VisitableInteger(5));
visitables.add(new VisitableString("Kevin"));
visitables.add(new VisitableInteger(17));
visitables.add(new VisitableString("Rocks"));
// Create a new collection to add to the main collection
Collection<Visitable> c = new ArrayList<Visitable>();
c.add(new VisitableInteger(15));
c.add(new VisitableString("Inner collection of death"));
visitables.add(new VisitableCollection(c));
// Iterate over the main collection and "act" on the data (remember that an element can "accept" a visitor, and the visitor will act on the value)
for (Visitable v : visitables)
{
v.accept(new DoSomethingVisitor());
v.accept(new LazyVisitor());
}
}
}
public interface Visitable
{
public void accept(Visitor v);
}
public interface Visitor
{
public void visit(VisitableInteger e);
public void visit(VisitableString e);
public void visit(VisitableCollection e);
}
public class VisitableCollection implements Visitable
{
private Collection<Visitable> c;
public VisitableCollection(Collection<Visitable> c)
{
this.c = c;
}
public Collection<Visitable> getValue()
{
return c;
}
public void accept(Visitor v)
{
v.visit(this);
}
}
public class VisitableInteger implements Visitable
{
private int i;
public VisitableInteger(int i)
{
this.i = i;
}
public int getValue()
{
return i;
}
public void accept(Visitor v)
{
v.visit(this);
}
}
public class VisitableString implements Visitable
{
private String s;
public VisitableString(String s)
{
this.s = s;
}
public String getValue()
{
return s;
}
public void accept(Visitor v)
{
v.visit(this);
}
}
public class DoSomethingVisitor implements Visitor
{
public void visit(VisitableInteger e)
{
System.out.println("I'm going to do all sorts of things with " + e.getValue() + " like double it (" + (e.getValue() * 2) + ")");
}
public void visit(VisitableString e)
{
System.out.println("Strings are pretty lame so I might as well just print the contents..." + e.getValue());
}
public void visit(VisitableCollection e)
{
System.out.println("What the fuck? Now I have a collection of a collection!");
for (Visitable v : e.getValue())
{
v.accept(this);
}
}
}
public class LazyVisitor implements Visitor
{
public void visit(VisitableInteger e)
{
System.out.println("I'm ignoring your int");
}
public void visit(VisitableString e)
{
System.out.println("I'm ignoring your String");
}
public void visit(VisitableCollection e)
{
System.out.println("Collections are kinda interesting, let's traverse it...");
for (Visitable v : e.getValue())
{
v.accept(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment