4

Currently I have some code like this:

    OntologyGenerator generator = new OntologyGenerator();
    generator.AddOntologyHeader("Testing");
    generator.AddClassDeclaration(owlBuilder);
    generator.AddSubClass(owlBuilder);
    generator.AddAnnotationAssertions(owlBuilder);

where that OwlBuilder param you see being passed has collections of objects like this:

public class OwlLBuilder: IOwlLBuilder
{
       private ICollection<IOwlClass> owlClasses = new Collection<IOwlClass>();
       private ICollection<IOwlRelation>  owlRelations = new Collection<IOwlRelation> ();
}

so for example when I say generator.AddClassDeclaration(owlBuilder); it will be looping through owlClasses collection of that owlBuilder param and do some stuff to it...

I feel it is an ugly design. Do you think I can take benefit of modifying my existing code to use Template Method Design Pattern or any other better design suggestions that you have? well with some code sample so I can have the big picture of what I should do in my head!

Manrico Corazzi
  • 11,299
  • 10
  • 48
  • 62
Bohn
  • 26,091
  • 61
  • 167
  • 254

1 Answers1

14

I really can't see Template Method in here. I see the Visitor pattern.

Here's an example.

Imagine you've got a bunch of houses, and you need them all to be painted yellow. Now you could just:

var houses = ... a list of houses ...

foreach (var house in houses)
{
    house.Color = Color.Yellow;
}

But instead of always painting your houses yellow, you may want the action (in this case painting) abstracted away into something else. One solution is to write a class that's responsible for painting a house yellow. That class could later be substituted for another class that paints the house in a different color, or does something completely different with your house, like adding another floor!

Enter the Visitor pattern.

I'll show some of the generic helper classes and interfaces I've been using. I like generics. If you don't, feel free to roll your own. The semantics are: "a visitor visits a visitable", and "a visitable accepts a visitor".

public interface IVisitor<in T> where T : IVisitable<T>
{
    void Visit(T visitable);
}

public interface IVisitable<out T> where T : IVisitable<T>
{
    void Accept(IVisitor<T> visitor);
}

public abstract class Visitable<T> : IVisitable<T> where T : Visitable<T>
{
    public void Accept(IVisitor<T> visitor)
    {
        visitor.Visit((T)this);
    }
}

public abstract class VisitableList<T> : List<T>, IVisitable<T> where T : Visitable<T>
{
    public void Accept(IVisitor<T> visitor)
    {
        foreach (var item in this)
        {
            item.Accept(visitor);
        }
    }
}

Now we can set up our house and our list of houses like this:

public class House : Visitable<House>
{
    public Color Color { get; set; }
}

public class Houses : VisitableList<House> {}

And now, the visitor - our painter - who can visit our visitable house:

public class YellowPainter : IVisitor<House>
{
    public void Visit(House visitable)
    {
        visitable.Color = Color.Yellow;
    }
}

Simple, elegant and a single responsibility(!).

Let's set up the houses:

var houses = new Houses();
houses.Add(new House() { Color = Color.Green });
houses.Add(new House() { Color = Color.Blue });
houses.Add(new House() { Color = Color.White });

Now we're ready to paint all of our houses. It only takes one call:

houses.Accept(new YellowPainter());

...and all our houses are now yellow. Nice!

We could just as easily do this:

houses.Accept(new AdditionalFloorsBuilder(floors: 2));

Or this:

owlClasses.Accept(new OwlClassVisitor(owlBuilder, ...));

By doing this, we've isolated what happens on the actual "visit" of each element in owlClasses, from the iteration of the collection itself. The visit needn't modify the visitable. It could be used to only inspect the visitable and use the information to modify something completely different, eg. you could use the information to feed your owlBuilder.

Christoffer Lette
  • 14,346
  • 7
  • 50
  • 58
  • 4
    Indeed. To the OP: big overlap with the Strategy pattern here (I'd personally call "YellowPainter" a "PaintYellowStrategy" . http://en.wikipedia.org/wiki/Strategy_pattern – Geert-Jan Aug 20 '12 at 23:01
  • holy crap thats the best example of the visitor pattern ive seen yet. – John Nicholas Aug 20 '12 at 23:20
  • Would you introduce the Visitor pattern right away here, or does it depend on the use case? In other words, is the Template Method pattern sufficient if there's little chance of variation? – neontapir Aug 21 '12 at 17:53
  • @neontapir As always, it depends on the context. Template Method *might* be sufficient, and hey, it might even be the correct choice in this particular scenario - there's not enough context for us to be (more) certain. The answer I gave is based on part experience (things might look simple now...) and part gut feeling. – Christoffer Lette Aug 21 '12 at 19:03
  • 1
    @ChristofferLette Agreed, the true intent of my question is to ask where's the tipping point? – neontapir Aug 22 '12 at 17:20
  • @neontapir You do raise an interesting question. I've never tried to "quantify" the decision before, but I'll give it a try. – Christoffer Lette Aug 22 '12 at 19:46
  • 2
    Both patterns support a way of plugging in behavior without modifying the original source. In TM, that original source is most often an abstract class which dictates call semantics and order. The subclass is tightly coupled to it's base, which means that more ways of interacting with the base are available to the subclass. – Christoffer Lette Aug 22 '12 at 19:46
  • 2
    (continued) In V, the visitor is "in charge", so control is inverted. There's also no imposed semantics from the visitable. However, the interaction with the visitable is limited to the public parts of the visitable's interface. The differences listed above (I've probably left some important ones out) could be used to tip the scale either way, depending on your needs. – Christoffer Lette Aug 22 '12 at 19:47
  • @neontapir I'm certainly no expert on the subject. Please add your thoughts. – Christoffer Lette Aug 22 '12 at 19:51
  • I don't profess to be an expert either. I under-utilize Visitor and was intrigued by your example, and I am looking for a heuristic or code smell for when to consider it. Templates normally represent an algorithm or recipe. Maybe Template Method is best when the algorithm is concrete, whereas Visitor is good when the algorithm is more variable. [This answer](http://stackoverflow.com/questions/4266750/whats-the-difference-between-the-patterns-strategy-visitor-and-template-method) opines that Visitor is good when traversing largely unrelated items. – neontapir Aug 22 '12 at 20:40
  • 2
    @neontapir It looks like we are in agreement. Either we're both right or we're both wrong! Cheers. ;-) – Christoffer Lette Aug 22 '12 at 20:57