Skip to Content.
Sympa Menu

gang-of-4-patterns - Re: [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

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


Chronological Thread 
  • From: oleg shteynbuk <oshteynbuk AT nyc.rr.com>
  • To: cfinlayson AT vls-inc.com
  • Cc: gang-of-4-patterns AT cs.uiuc.edu
  • Subject: Re: [gang-of-4-patterns] Benefits of the visitor pattern in Go4's example - I don't understand
  • Date: Fri, 19 Mar 2004 19:44:47 -0500
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

Example that you describe is from the “Sample Code” of the Visitor and in the first paragraph of this section you can find: “The Equipment classes are so simple that using Visitor isn't really necessary, but they make it easy to see what's involved in implementing the pattern”. They probably anticipated your email :) Motivation section has other example that shows that advantage of the Visitor.

Oleg



Chris Finlayson wrote:

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.


_______________________________________________
gang-of-4-patterns mailing list
gang-of-4-patterns AT cs.uiuc.edu
http://mail.cs.uiuc.edu/mailman/listinfo/gang-of-4-patterns







Archive powered by MHonArc 2.6.16.

Top of Page