troubleshooting Question

use of visitor data pattern

Avatar of perlperl
perlperl asked on
4 Comments1 Solution385 ViewsLast Modified:
I have been reading  a lot last few days about visitor data pattern but my head just can't digest it ;) on how to use it in my application. I came across a situation in my application code, where I think I might be able to make it more robust and efficient and visitor pattern came to mind. However I wasn't sure if I can apply it or not. I don't want use it just for the heck of using it ;)

Here's my existing design:
1) Virtual Base class with one pure virtual function:
        class Acc {     
                  virtual uint32_t common_method(uint32_t arg1, uint32_t arg2) = 0;

2) There are 8 different classes derived from this Base class "Acc" and 
each of them has its own implementation of common_method(arg1, arg2);

        class Acc_Der_1 : public Acc;
        class Acc_Der_2 : public Acc;
        class Acc_Der_8 : public Acc;
3) One Factory class that returns an object of one of the above 8 classes
 based on some condition check.

        class Acc_factory {
            Acc* create_obj(uint32_t type) {
                   if (cond_1)
                       return new Acc_Der_1();
                   if (cond_2)
                       return new Acc_Der_2();
                   ........................ /// so on .....
                   if (cond_8)
                       return new Acc_Der_8();

4) Finally a manage class which invokes the actual virtual method. 

     class Acc_Manager {
          void main_func(uint32_t type) {
              std::auto_ptr<Acc*> acc_obj (Acc_factory::get_instance->create_obj(type))
              acc_obj->common_method(some_arg1, some_arg2); 

Now I need to extend my application to add 4 more derived classes from Acc
       class Acc_Der_9 : public Acc;
       class Acc_Der_10 : public Acc;
       class Acc_Der_11 : public Acc;
       class Acc_Der_12 : public Acc;

Each of them will have their own implementation of common_method(Arg1, arg2);

This will also force me to update the factory class to add 4 more condition check to return the correct derived class object.

Based on this requirement, is it feasible to use "Visitor Design Pattern". If so what are the pros and cons of it.
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros