Skip to Content.
Sympa Menu

gang-of-4-patterns - [gang-of-4-patterns] Benefits of the visitor pattern in Go4's example - I don't understand

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

Subject: Design Patterns discussion

List archive

[gang-of-4-patterns] Benefits of the visitor pattern in Go4's example - I don't understand


Chronological Thread 
  • From: "Chris Finlayson" <cfinlayson AT vls-inc.com>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: [gang-of-4-patterns] Benefits of the visitor pattern in Go4's example - I don't understand
  • Date: Fri, 19 Mar 2004 16:05:49 -0700
  • Importance: Normal
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Hi:

I don't understand the advantage a Visitor pattern provides in the context
of the Go4 example - I was wondering if someone could help clarify this for
me.

If one has a composite - such as the Equipment composite that the Go4
discuss and one wants to compute the cost of equipment, I don't get why the
PricingVisitor is advantageous. Why bother with the double-dispatch
mechanism over this well-defined Composite interface?

For example, instead of the PricingVisitor, we could have something like:

class EquipmentPricingPolicy
{

public:
EquipmentPricingPolicy(Equipment* oEquipment)
{
m_oEquipment = oEquipment;
}
Currency GetNetPrice()
{
return m_oEquipment->NetPrice();
}

Currency GetDiscountPrice()
{
return m_oEquipment->DiscountPrice();
}

Currency GetSavings()
{
return GetNetPrice() - GetDiscountPrice();
}

//etc. etc.

private:
Equipment* m_oEquipment
}

So, when m_oEquipment->NetPrice() is called, it gets the net price of the
equipment - since Equipment is implemented following the Composite design
pattern. This seems more straightforward.


What I CAN understand is how a visitor is applicable in working with many
disparate classes of distinct interfaces. I.e., if one is piecing together
disparate modules of some legacy code, each having their own definition of
an Equipment and how to return its net price. I do not, however, understand
how it is advantageous for a set of classes adhering to a well-defined
interface, as the Equipment class' adhere to.


--Chris.






Archive powered by MHonArc 2.6.16.

Top of Page