Jump to content

  • Log In with Google      Sign In   
  • Create Account

Visitor pattern implementation.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 rozz666   Members   -  Reputation: 650

Like
0Likes
Like

Posted 01 October 2009 - 11:46 PM

I found an easy way to use the Visitor pattern using CRTP. I googled around, but haven't found such implementation, so I'm asking if somebody here have used a similar aproach.
class VisitorInterface {
public:
    virtual void visit(const Object1& ) = 0;
    virtual void visit(const Object2& ) = 0;
    virtual void visit(const Object3& ) = 0;
};

template <typename Derived>
class VisitorImplementation : public VisitorInterface {
public:

    virtual void visit(const Object1& o) { static_cast<Derived&>(*this).visitImpl(o); }
    virtual void visit(const Object2& o) { static_cast<Derived&>(*this).visitImpl(o); }
    virtual void visit(const Object3& o) { static_cast<Derived&>(*this).visitImpl(o); }
};

// concrete visitor

class MyVisitor : public VisitorImplementation<MyVisitor> {
public:

    template <typename Object>
    void visitImpl(Object& o)
    {
       // ...
    }
};


You have to implement only one method - visitImpl [Edited by - rozz666 on October 2, 2009 7:03:45 AM]

Sponsor:

#2 ofingle   Members   -  Reputation: 139

Like
0Likes
Like

Posted 02 October 2009 - 12:16 AM

Doesn't that defeat the purpose of the visitor?
How are you going to differentiate between the different types unless you overload/specialize visitImpl?

#3 rozz666   Members   -  Reputation: 650

Like
0Likes
Like

Posted 02 October 2009 - 12:58 AM

Quote:
Original post by ofingle
Doesn't that defeat the purpose of the visitor?
How are you going to differentiate between the different types unless you overload/specialize visitImpl?


You can use overloading and/or specialization.
But the problem it solves is different. Suppose you have traits for you Objectx types. You can't use them without adding virtual methods to your base class and implementations to all you objects that look all the same. Using my approach you just need to do it once.
Moreover, you can group objects of different types without having a common base class, using e.g. Boost.EnableIf.

#4 ofingle   Members   -  Reputation: 139

Like
0Likes
Like

Posted 02 October 2009 - 01:24 AM

Quote:
Original post by rozz666
Quote:
Original post by ofingle
Doesn't that defeat the purpose of the visitor?
How are you going to differentiate between the different types unless you overload/specialize visitImpl?


You can use overloading and/or specialization.
But the problem it solves is different. Suppose you have traits for you Objectx types. You can't use them without adding virtual methods to your base class and implementations to all you objects that look all the same. Using my approach you just need to do it once.
Moreover, you can group objects of different types without having a common base class, using e.g. Boost.EnableIf.


That's true. I've never used a Visitor like that, but I guess it can be useful.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS