On Wed, 2003-05-28 at 21:24, Yves Roy wrote:
> 1) The GOF book mentions in the Related Patterns section of Prototype
> pattern (p. 126) that "Design that make heavy use of the Composite and
> Decorator patterns often can benefit from Prototype as well".
>
> My question is, how, exactly? Any simple C++ examples illustrating would
> be welcome.
Decorator doesn't know wich object is decorated exactly. For example:
border can decorate any heir of visual object. We have picture and text
(both visual). User can decorate a picture with border, then he want to
copy this bordered picture. At the same time somewhere in program...
client creates copy of the border, but border doesn't know wich object
it must create. It has pointer to visual object (ancestor) instead of
picture (heir). Solution is to add clone method to ancestor class (in
other words to make ancestor prototype). Now border can clone itself and
decorated object.
class Visual_object {
public:
virtual void draw() = 0;
virtual Visual_object* clone() = 0;
};
class Border_decorator: public Visual_object {
public:
Border_decorator(Visual_object* c): _component(c) {}
virtual void draw()
{
_component->draw();
draw_border();
}
virtual Visual_object* clone()
{
return new Border_decorator( _component->clone() );
}
private:
Visual_object* _component;
};
class Picture: Visual_object {
public:
virtual void draw()
{
draw_picture();
}
}
class Text: Visual_object {
public:
virtual void draw()
{
draw_text();
}
}
Concerning Composite. Composite doesn't know wich object is contained
exactly... and so on...
--
Andrew Chelyabin <[EMAIL PROTECTED]>
_______________________________________________
patterns-discussion mailing list
[EMAIL PROTECTED]
http://mail.cs.uiuc.edu/mailman/listinfo/patterns-discussion