Skip to Content.
Sympa Menu

gang-of-4-patterns - [gang-of-4-patterns] Polymorph Visitor vs Reusability

gang-of-4-patterns AT lists.cs.illinois.edu

Subject: Design Patterns discussion

List archive

[gang-of-4-patterns] Polymorph Visitor vs Reusability


Chronological Thread 
  • From: "Bangels Stefan" <fader AT rootshell.be>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: [gang-of-4-patterns] Polymorph Visitor vs Reusability
  • Date: Wed, 3 Dec 2003 22:24:00 +0100
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Hello All,

Some time ago, I stumbled upon a problem with reusability in coöperation
with the polymorph visitor. Now, I've found a solution, but have doubts
about the remedy's quality. In this mail, I'd like to discuss the problem
and the answers that I've found. All replies are welcome!


SCENARIO
--------

I have two classes, A and B, inheriting from an abstract class C.

In the model, the class C is being used as an abstract interface for classes
A and B.

The three classes, A, B and C are all canditates for reusability in other
projects. However, the other projects might not necessairily need both
classes A and B,
they might just need one.


THE PROBLEM
-----------

I need a visitor, to add database functionality to classes A and B and
future subclasses of C.

BUT

My other projects might not need that visitor, so I can't define the
visitor-accept-methods (load and store) in the abstract class C.

So... for reusability reasons, I can't define the methods Store(..) and
Load(..) in the classes A, B, C. I can't call different visitor-methods for
different classes A, B, etc.


MY SOLUTION
-----------

To take care of this problem, I created a decorator class D, to decorate
descendants of C, that are in need of the visitor.

This decorator, adds the methods :

D::Store(cDatabaseWriterVisitor * DBWR) { DBWR->Store((C
*)this->_decoratedcomponent); }
D::Load(cDatabaseWriterVisitor * DBWR) { DBWR->Load((C
*)this->_decoratedcomponent); }

to classes A or B.


THE PROBLEM WITH MY SOLUTION
----------------------------

I implemented "OVERLOADING"-methods in the visitor E:

E::Store(A *);
E::Store(B *);
E::Load(A *);
E::Load(B *);

instead of the methods

E::StoreA(A *);
E::StoreB(B *);
E::LoadA(A *);
E::LoadB(B *);

Instead of polymorph methods in the concrete classes, I use polymorph
methods in the visitor.

*** My solution might also help to implement a default visitor for new
subclasses of C.

*** It can also be used to define pluggable visitor-behavour, dependent of
the needs of your current project. (you might need the visitor functionality
only for a short time, later on it's only filling memory, example: adding
database behaviour)


Is this a legitimate solution for my problem ???

Is there an other way ???

Is overloading methods, next to C++, portable with most other OO-languages
???


I am looking forward to your replies!

Kind regards,

Stefan Bangels





  • [gang-of-4-patterns] Polymorph Visitor vs Reusability, Bangels Stefan, 12/03/2003

Archive powered by MHonArc 2.6.16.

Top of Page