Skip to Content.
Sympa Menu

patterns-discussion - Re: [patterns-discussion] Prototype and Composite/Decorator and Builder for Composite

patterns-discussion AT lists.siebelschool.illinois.edu

Subject: General talk about software patterns

List archive

Re: [patterns-discussion] Prototype and Composite/Decorator and Builder for Composite


Chronological Thread 
  • From: Andrew Chelyabin <chelyabin AT tspotest.comch.ru>
  • To: "patterns-discussion AT cs.uiuc.edu" <patterns-discussion AT cs.uiuc.edu>
  • Subject: Re: [patterns-discussion] Prototype and Composite/Decorator and Builder for Composite
  • Date: 29 May 2003 16:24:07 +0400
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/patterns-discussion/>
  • List-id: General talk about software patterns <patterns-discussion.cs.uiuc.edu>
  • Organization:

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
<chelyabin AT tspotest.comch.ru>





Archive powered by MHonArc 2.6.16.

Top of Page