Saturday 16 June 2018

strategy design pattern

A Strategy defines a set of algorithms that can be used interchangeably. Modes of transportation to an airport is an example of a Strategy. Several options exist such as driving one's own car, taking a taxi, an airport shuttle, a city bus, or a limousine service.


------------------------
#include
using namespace std;

class vehicle
{
public:
        virtual void useVehicle(){};

};
class ford:public vehicle
{
public:
        void useVehicle()
        {
                cout<<"driving ford"<        }

};
class tata:public vehicle
{
public:
        void useVehicle()
        {
                cout<<"driving tata"<        }
};
class family
{
        public:
                family(){};
                vehicle *myVehicle;
                void setVehicle(vehicle *v)
                {
                        myVehicle = v;
                }
                                                 


                }
                void driveVehicle()
                {
                     myVehicle->useVehicle();
                }
                virtual void display(){cout<<"i am family"<};
class father:public family
{
public:
        father(){};
        ~father(){};
        void display(){cout<<"I am a father"<
};
class son:public family
{
public:
        son(){};
        ~son(){};
        void display(){cout<<"I am a son"<
};

int main(int argc, char *argv[])
{
        family *fm = new father();
        fm->display();
        fm->setVehicle(new ford());
        fm->driveVehicle();
        delete fm;
}
                                                                                                                                 

No comments:

Post a Comment