Hi,

not so much into software design patterns [further - SDP], so while learning this "thing" i have some issues with trying to combine multiple SDP. I think that there are some logical issues..

Thank you for your time! :)

#include <iostream>
 
using namespace std;
 
/*the Command interface*/
class Command 
{
public:
        virtual void execute()=0;
};
 
/*Receiver class*/
class Light {
 
public:
        Light() {  }
 
        void turnOn() 
        {
                cout << "The light is on" << endl;
        }
 
        void turnOff() 
        {
                cout << "The light is off" << endl;
        }
};
 
/*the Command for turning on the light*/
class FlipUpCommand: public Command 
{
public:
 
        FlipUpCommand(Light& light):theLight(light)
        {
 
        }
 
        virtual void execute()
        {
                theLight.turnOn();
        }
 
private:
        Light& theLight;
};
 
/*the Command for turning off the light*/
class FlipDownCommand: public Command
{
public:   
        FlipDownCommand(Light& light) :theLight(light)
        {
 
        }
        virtual void execute() 
        {
                theLight.turnOff();
        }
private:
        Light& theLight;
};
 
class Switch {
public:
        Switch(Command& flipUpCmd, Command& flipDownCmd)
        :flipUpCommand(flipUpCmd),flipDownCommand(flipDownCmd)
        {
 
        }
 
        void flipUp()
        {
                flipUpCommand.execute ( ) ;
        }
 
        void flipDown()
        {
                flipDownCommand.execute ( ) ;
        }
 
private:
        Command& flipUpCommand;
        Command& flipDownCommand;
};

class Decorator: public Switch  // 4. "is a" relationship
{
  public:
    Decorator ( Switch * sw ) : swit ( *sw )
    {
        swit = sw;
    }
 
    /*virtual*/
    void flipUp ( ) 
    {
        swit -> flipUp ( ) ; // 5. Delegation
	}
	void flipDown ( ) 
    {
        swit -> flipDown ( ) ; // 5. Delegation
	}
private:
    Switch * swit ; // 4. "has a" relationship
};

class TextColorDecorator: public Decorator
{
  public:
    // 6. Optional embellishment
    TextColorDecorator ( Switch * sw ) : Decorator ( sw )
	{
	}
    /*virtual*/
    void flipUp ( )
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::flipUp ( ) ;
        cout << "   TextColorDecorator" << '\n';
    }
	void flipDown ( )
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::flipDown ( ) ;
        cout << "   TextColorDecorator" << '\n';
    }
};

class TextSizeDecorator: public Decorator
{
  public:
    // 6. Optional embellishment
    TextSizeDecorator ( Switch * sw ) : Decorator ( sw )
	{
	}
    /*virtual*/
    void flipUp ( )
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::flipUp ( ) ;
        cout << "   TextSizeDecorator" << '\n';
    }
	void flipDown ( )
    {
        // 7. Delegate to base class and add extra stuff
        Decorator::flipDown ( ) ;
        cout << "   TextSizeDecorator" << '\n';
    }
};
 
/*The test class or client*/
int main() 
{
        Light lamp;
        FlipUpCommand switchUp(lamp);
        FlipDownCommand switchDown(lamp);
        Switch s(switchUp, switchDown);
		Decorator dec( new TextColorDecorator ( new TextSizeDecorator ( &s ) ) );
        dec.flipUp();
        dec.flipDown();
}

g++ error list:

testpattern.cpp: In constructor ‘Decorator::Decorator(Switch*)’:
testpattern.cpp:90:44: error: no matching function for call to ‘Switch::Switch()’
testpattern.cpp:90:44: note: candidates are:
testpattern.cpp:66:9: note: Switch::Switch(Command&, Command&)
testpattern.cpp:66:9: note:   candidate expects 2 arguments, 0 provided
testpattern.cpp:64:7: note: Switch::Switch(const Switch&)
testpattern.cpp:64:7: note:   candidate expects 1 argument, 0 provided
testpattern.cpp:90:44: error: cannot convert ‘Switch’ to ‘Switch*’ in initialization

In lines 90-93, change

Decorator ( Switch * sw ) : swit ( *sw )
    {
        swit = sw;
    }

To

Decorator ( Switch * sw ) : swit ( sw ) // initialize the [B]pointer[/B] swit with sw
{}

In lines 90-93, change

Decorator ( Switch * sw ) : swit ( *sw )
    {
        swit = sw;
    }

To

Decorator ( Switch * sw ) : swit ( sw ) // initialize the [B]pointer[/B] swit with sw
{}

Same problem.... :

testpattern.cpp: In constructor ‘Decorator::Decorator(Switch*)’:
testpattern.cpp:90:43: error: no matching function for call to ‘Switch::Switch()’
testpattern.cpp:90:43: note: candidates are:
testpattern.cpp:66:9: note: Switch::Switch(Command&, Command&)
testpattern.cpp:66:9: note:   candidate expects 2 arguments, 0 provided
testpattern.cpp:64:7: note: Switch::Switch(const Switch&)
testpattern.cpp:64:7: note:   candidate expects 1 argument, 0 provided
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.