Skip to Content.
Sympa Menu

gang-of-4-patterns - Re: [gang-of-4-patterns] Prototype and Composite/Decorator and Builder for Composite

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

Subject: Design Patterns discussion

List archive

Re: [gang-of-4-patterns] Prototype and Composite/Decorator and Builder for Composite


Chronological Thread 
  • From: "foxgem" <jianhgreat AT hotmail.com>
  • To: <gang-of-4-patterns AT cs.uiuc.edu>
  • Subject: Re: [gang-of-4-patterns] Prototype and Composite/Decorator and Builder for Composite
  • Date: Thu, 29 May 2003 15:00:27 +0800
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/gang-of-4-patterns/>
  • List-id: Design Patterns discussion <gang-of-4-patterns.cs.uiuc.edu>

> I have 2 clear and simple questions:
>
> 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.
>
suppose u want to get a copy of a Composite, hope this code will be helpful
to u.

class Component{
public:
virtual Component* Clone()=0;
virtual ~Component();
virtual void Add( Componet*)=0;
virtual void Remove( Component*)=0;
.............
};

class Composite: public Component
{
public:
......
inline void Add( Componet* co){
m_Children.pushback( co);
}
private:
vector< Composite*> m_Children;
};

Component* Composite::Clone(){
vector< Composite*>::iterator it;
Composite* co= new Composite();

//Clone the Children
for(it = m_Children.begin(); it != m_Children.end(); it++)
co->Add( (*it)->Clone());

//Assign some property of this object;

return co;
}

class Leaf: public Component
{
........
};

Component* Leaf::Clone(){
Leaf* leaf= new Leaf();

//Assign some property of this object;

return leaf;
}





Archive powered by MHonArc 2.6.16.

Top of Page