Showing posts with label designPatterns. Show all posts
Showing posts with label designPatterns. Show all posts

Saturday, March 31, 2012

Purpose-Specific Visitors

This is a follow-up to my "Why visitor pattern is worth the overhead" entry. I'll be referring to the example in that entry...

One thing that's bothered me about the Visitor pattern is the syntax it introduces. In the example, our core domain is the concept of Feedback. On a Feedback object, you would expect to see methods/operations related to that problem domain.

However, if you implement the "textbook" Visitor pattern, you end up with:

// C# pseudocode...

abstract class Feedback
{
    void Accept(IFeedbackVisitor visitor);

    // Other domain meaningful methods
}

The 'accept' method is questionable. In the best case scenario, the word is meaningless to the domain, and we simply dismiss it as an annoying requirement of the pattern. In the worst case scenario, the word is meaningful to the domain but is a completely unrelated concept. So in this example, we could be misled to think that this method helps us accept or receive feedback from a user.

Let's express our true intention:

abstract class Feedback
{
    void DescribeTo(IFeedbackRenderer renderer);

    // Other domain meaningful methods
}

class StandardChoiceFeedback : Feedback
{
    int code;
    string description;
    void DescribeTo(IFeedbackRenderer renderer) { renderer.Render(description); }
}

class FreeFormFeedback : Feedback
{
    string text;
    void DescribeTo(IFeedbackRenderer renderer) { renderer.Render(text); }
}

class FollowUpCallFeedback : Feedback
{
    PhoneNumber phoneNumber;
    void DescribeTo(IFeedbackRenderer renderer) { renderer.Render(phoneNumber); }
}

// *******************************

interface IFeedbackRenderer
{
    void Render(String text);
    void Render(PhoneNumber phoneNumber);
}

Now, the purpose of the visitor is clear - we're getting a Feedback object to describe itself to another object that will help render it for display purposes.

I actually went one step further with this. Following "textbook" Visitor would have given us:

interface IFeedbackRenderer
{
    void Render(StandardChoiceFeedback feedback);
    void Render(FreeFormFeedback feedback);
    void Render(FollowUpCallFeedback feedback);
}

That's fine too. What I've done instead, is 'inline' the visitor methods to reflect what a Renderer would really need to do its rendering. An interesting effect with this approach is that the internals of each type of Feedback stay unexposed - no getters are required to make this work.

Wednesday, August 16, 2006

Why visitor pattern is worth the overhead

I've observed that many developers tend to shy away from the visitor pattern. I suspect it's because even though they understand the pattern, the double-dispatch and the seemingly out-of-place accept methods seem like too much overhead. So, instead of using visitor, we end up with logic that switches on a simple enumeration or the type of an object. Compared to visitor, this kind of code is arguably simpler to understand.

I think that visitors are worth the overhead. What we gain is the ability to easily identify the effects of type changes.

Let's use this example: One part of our application captures user feedback. They have two options:
  • Choose a feedback from a standard list (eg. "Good" or "Bad")
  • Submit a short sentence describing their usage experience.


// C# pseudocode...

abstract class Feedback
{
    void Accept(IFeedbackVisitor visitor);
}

class StandardChoiceFeedback : Feedback
{
    int Code;
    string Description;
    void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }
}

class FreeFormFeedback : Feedback
{
    string Text;
    void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }
}

interface IFeedbackVisitor
{
    void Visit(StandardChoiceFeedback feedback);
    void Visit(FreeFormFeedback feedback);
}


Somewhere in the system, we display a grid of data, where each row corresponds to a user and one of the columns shows their feedback:

class FeedbackColumnRenderer : IFeedbackVisitor
{
    string RenderedText;

    void Visit(StandardChoiceFeedback feedback)
    {
        RenderedText = feedback.Description;
    }

    void Visit(FreeFormFeedback feedback)
    {
        RenderedText = feedback.Text;
    }
}

string RenderFeedbackColumn(Feedback feedback)
{
    FeedbackColumnRenderer renderer = new FeedbackColumnRenderer();
    feedback.Accept(renderer);
    return renderer.RenderedText;
}


So far, all we've shown is the overhead incurred by using visitor.

However, what happens when the requirements change? For example, our business analyst comes back in 3 weeks to inform us that on top of the two feedback options, there needs to be another option — the user can choose to leave their phone number so that a customer representative can give them a follow-up call to chat about their experience. We are asked to implement this change in the UI and the persistence layer.

Our core types change like this:

class FollowUpCallFeedback : Feedback
{
    PhoneNumber phoneNumber;
    void Accept(IFeedbackVisitor visitor) { visitor.Visit(this); }
}

interface IFeedbackVisitor
{
    void Visit(StandardChoiceFeedback feedback);
    void Visit(FreeFormFeedback feedback);
    void Visit(FollowUpCallFeedback feedback);
}


With just these changes, let's re-compile. What we find is that compilation isn't successful because all implementors of IFeedbackVisitor now have to implement the new Visit(FollowUpCallFeedback) method. On further examination of the compiler errors, we discover that not only are there feedback visitors in the UI and persistence layers, but there is also one used for an obscure report. We talk to our business analyst again, and find out that this report has been missed in talking about the change. Great! We then talk about what the report should look like when a phone number is involved.

I think this illustrates the power of the pattern. When something changes in our type hierarchy, we are actually forced to address all the places which will be affected up-front. If we had chosen to use a simple enumeration or type switching scheme instead and we are not extremely careful, we might not discover all the places that are affected by the requirements change until much later (for instance, when the testers [or worse, the users] visually inspect that particular report).