Hi.
I am learning Design Patterns so I pulled this piece of code off the
ng (decorator) . Now I am going through it with the debugger and
seeing what does what and I have found something I just don't
understand.
The constructor for CCoffeeDecorato r has this in the list
CCoffeeComponen t().
I see it hitting the CCoffeeComponen t() constructor ... but I can't
figure out what this accomplishes. The only thing I can think of, is
that this sets the pointer type to CCoffeeComponen t ... but I don't
understand the mechanics are behind it.
Randy
#include <iostream>
#include <string>
using namespace std;
/*
*************** *************** *************** *************** *************** *************** *************
abstract base class ** normal - any class can be decorated.
hide constructor prevent construction of a plain component
*
*************** *************** *************** *************** *************** *************** **************/
class CCoffeeComponen t {
public:
virtual string Info() = 0;
protected:
CCoffeeComponen t () {};
string m_Info;
};
/*
*************** *************** *************** *************** *************** *************** *************
Furthermore we need a decorator class that "is-a" component and
stores a
pointer to a passed component object:
NOTE: the default NULL-pointer that is used as an end-marker of
the component chain
*
*************** *************** *************** *************** *************** *************** **************/
class CCoffeeDecorato r : public CCoffeeComponen t {
public:
CCoffeeDecorato r( CCoffeeComponen t* pComponent = 0) :
CCoffeeComponen t(), m_pComponent( pComponent ) {};
public:
virtual string Info() {
if( !m_pComponent )
return string("");
return m_pComponent->Info();
}; // delegate info call to actual
implementation
protected:
CCoffeeComponen t* m_pComponent;
};
//This base implementation of the decorator delegates any Info() calls
to its
//wrapped component if there is any. In principle we've got the tools
to
//create different component combinations at our fingertips. Now we
get down
//the some sample component implementations :
// create different decorator implementations
class CEspresso: public CCoffeeDecorato r
{
public:
CEspresso( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string("
espresso"); };
};
// create different decorator implementations
class CSteamedMilk: public CCoffeeDecorato r
{
public:
CSteamedMilk( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string(" steamed
milk"); };
};
// create different decorator implementations
class CFoamedMilk: public CCoffeeDecorato r
{
public:
CFoamedMilk( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string(" foamed
milk"); };
};
class CSugar: public CCoffeeDecorato r
{
public:
CSugar( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r( pComponent ){};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string("
sugar"); };
};
class CMug: public CCoffeeDecorato r
{
public:
CMug( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r( pComponent ){};
// extend info implementation and delegate to base class
string Info() {
return CCoffeeDecorato r::Info() + string(" mug");
};
};
int main()
{
/*
The pointer type is component, however the object type is
Decorator. This is the
jist of virtual/polymorphism.
The recursion here is because the constructor for the decorator is
*/
CCoffeeComponen t* pCappucino= new CEspresso( new CSugar( new
CFoamedMilk( new CMug) ) );
CCoffeeComponen t* pMocca = new CEspresso( new CSugar( new CMug ) );
CCoffeeComponen t* pEmpty = new CMug;
cout << "Cappucino components: ";
cout << pCappucino->Info() << endl;
cout << "Mocca components: ";
cout << pMocca->Info() << endl;
cout << "Empty components: ";
cout << pEmpty->Info() << endl;
delete pCappucino;
delete pMocca;
system("PAUSE") ;
return EXIT_SUCCESS;
return 0;
}
I am learning Design Patterns so I pulled this piece of code off the
ng (decorator) . Now I am going through it with the debugger and
seeing what does what and I have found something I just don't
understand.
The constructor for CCoffeeDecorato r has this in the list
CCoffeeComponen t().
> CCoffeeDecorato r( CCoffeeComponen t* pComponent = 0) : CCoffeeComponen t(), m_pComponent( pComponent ) {};
figure out what this accomplishes. The only thing I can think of, is
that this sets the pointer type to CCoffeeComponen t ... but I don't
understand the mechanics are behind it.
Randy
#include <iostream>
#include <string>
using namespace std;
/*
*************** *************** *************** *************** *************** *************** *************
abstract base class ** normal - any class can be decorated.
hide constructor prevent construction of a plain component
*
*************** *************** *************** *************** *************** *************** **************/
class CCoffeeComponen t {
public:
virtual string Info() = 0;
protected:
CCoffeeComponen t () {};
string m_Info;
};
/*
*************** *************** *************** *************** *************** *************** *************
Furthermore we need a decorator class that "is-a" component and
stores a
pointer to a passed component object:
NOTE: the default NULL-pointer that is used as an end-marker of
the component chain
*
*************** *************** *************** *************** *************** *************** **************/
class CCoffeeDecorato r : public CCoffeeComponen t {
public:
CCoffeeDecorato r( CCoffeeComponen t* pComponent = 0) :
CCoffeeComponen t(), m_pComponent( pComponent ) {};
public:
virtual string Info() {
if( !m_pComponent )
return string("");
return m_pComponent->Info();
}; // delegate info call to actual
implementation
protected:
CCoffeeComponen t* m_pComponent;
};
//This base implementation of the decorator delegates any Info() calls
to its
//wrapped component if there is any. In principle we've got the tools
to
//create different component combinations at our fingertips. Now we
get down
//the some sample component implementations :
// create different decorator implementations
class CEspresso: public CCoffeeDecorato r
{
public:
CEspresso( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string("
espresso"); };
};
// create different decorator implementations
class CSteamedMilk: public CCoffeeDecorato r
{
public:
CSteamedMilk( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string(" steamed
milk"); };
};
// create different decorator implementations
class CFoamedMilk: public CCoffeeDecorato r
{
public:
CFoamedMilk( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r(pComponent ) {};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string(" foamed
milk"); };
};
class CSugar: public CCoffeeDecorato r
{
public:
CSugar( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r( pComponent ){};
// extend info implementation and delegate to base class
string Info() { return CCoffeeDecorato r::Info() + string("
sugar"); };
};
class CMug: public CCoffeeDecorato r
{
public:
CMug( CCoffeeComponen t* pComponent = 0) :
CCoffeeDecorato r( pComponent ){};
// extend info implementation and delegate to base class
string Info() {
return CCoffeeDecorato r::Info() + string(" mug");
};
};
int main()
{
/*
The pointer type is component, however the object type is
Decorator. This is the
jist of virtual/polymorphism.
The recursion here is because the constructor for the decorator is
*/
CCoffeeComponen t* pCappucino= new CEspresso( new CSugar( new
CFoamedMilk( new CMug) ) );
CCoffeeComponen t* pMocca = new CEspresso( new CSugar( new CMug ) );
CCoffeeComponen t* pEmpty = new CMug;
cout << "Cappucino components: ";
cout << pCappucino->Info() << endl;
cout << "Mocca components: ";
cout << pMocca->Info() << endl;
cout << "Empty components: ";
cout << pEmpty->Info() << endl;
delete pCappucino;
delete pMocca;
system("PAUSE") ;
return EXIT_SUCCESS;
return 0;
}
Comment