Header Ads

Decorator in C++

Code below explains each and everything about Decorators in C++:
Note This code is written by my Good friend from DSU.


#include
using namespace std;
class Weapon{
 public:
    virtual void fire()=0; // This function is virtual because we will derive several classes
                            // From weapon
};
// Hand gun is inherited from Weapon because handgun is a type of weapon

class Handgun: public Weapon{
    void fire(){    //In the derived class we will define the fire function explicitly for the Handgun
        cout<<"Handgun fires ";
    }
};
// Here comes the decorator this decorator assigns additional responsibilities at runtime to a weapon object
//This decorator must be inherited from the base class weapon so that an object of type weapon can be
// passed to the decorator on which the decorator is going to perform the action
class decorator: public Weapon{
    protected :
    Weapon *weaponPtr; // A point of type base is created in decorator to get the object which is going to be decorated
public:
    decorator(Weapon* weaponPtr){   //at run time the weapon which is going to be decorated would be passed to the
                                    // Constructor of decorator
        this->weaponPtr=weaponPtr;
    }

    //Now look at the function of fire in decorator
    virtual void fire(){    //Virtual because this is the base class of decorator further decorator classes would be
                            //derived
        weaponPtr->fire();  //The weapon object which would come in the weaponPtr its fire function would be called
    }
};
//Now here comes different types of decorators
//Derive all the different type of decorators from decorators base class
class silencerDecorator: public decorator {
public:
    silencerDecorator(Weapon* weapon):decorator(weapon){} //The weapon object must be passed to constructor of the base class
    void fire(){
        decorator::fire(); // this function call will invoke simple fire of the weapon
        cout<<" with silencer "; //Here some addition responsibilty is added to the fire of weapon by decorator
                                    //which is with silencer
    }
};
class laserDecorator: public decorator {
public:
    laserDecorator(Weapon* weapon):decorator(weapon){} //The weapon object must be passed to constructor of the base class
    void fire(){
        decorator::fire(); // this function call will invoke simple fire of the weapon
        cout<<" with laser "; //Here some addition responsibilty is added to the fire of weapon by decorator
                                    //which is with laser
    }
};
int main(){
    //You want a handgun with silencer and with laser then here is the code
    decorator *glockWithSilencer=new laserDecorator(new silencerDecorator(new Handgun)); //a handgun would be decorated with silencer
    //check this
    glockWithSilencer->fire();
    //The handgun will fire with silencer
}


1 comment:

  1. I think I have really come on the right place for getting the perfect info.
    tutorial for c++

    ReplyDelete