Strategy Design Pattern
This is one of behavioural design pattern.
The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements.
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time. This allows the behavior of a program to change dynamically according to configuration details or user preferences. It also increases flexibility by allowing new algorithms to be easily incorporated in the future.
2. Implements the behaviors in derived classes.
3. Define a strategy class which aggregates with behavior class.
Example: While traveling, we can choose any of transport service; bus, cab, train, bike. Each travel service have their estimate time & cost of travel. Traveler can run timely choose which service he/she can use while traveling.
#include<iostream>
using namespace std;
class Vehicle
{
protected:
int costperkm;
public:
virtual void Travel(int distance)=0;
};
class Cab : public Vehicle
{
public:
Cab()
{
costperkm = 10;
}
void Travel(int distance)
{
cout<<"Trave by Cab : ";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Bus : public Vehicle
{
public:
Bus()
{
costperkm = 5;
}
void Travel(int distance)
{
cout<<"Travle by Bus : ";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Train : public Vehicle
{
public:
Train()
{
costperkm = 2;
}
void Travel(int distance)
{
cout<<"Travel by Train :";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Travel
{
Vehicle *trans;
public:
void SetVehicle(Vehicle *v)
{
trans = v;
}
void TravelUser(int distance)
{
trans->Travel(distance);
}
};
int main()
{
Vehicle *cab = new Cab;
Travel t;
t.SetVehicle(cab);
t.TravelUser(20);
}
The strategy pattern is a design pattern that allows a set of similar algorithms to be defined and encapsulated in their own classes. The algorithm to be used for a particular purpose may then be selected at run-time according to your requirements.
The strategy pattern is used to create an interchangeable family of algorithms from which the required process is chosen at run-time. This allows the behavior of a program to change dynamically according to configuration details or user preferences. It also increases flexibility by allowing new algorithms to be easily incorporated in the future.
Checklist
1. Create a abstract base class which contains behaviors.2. Implements the behaviors in derived classes.
3. Define a strategy class which aggregates with behavior class.
Example: While traveling, we can choose any of transport service; bus, cab, train, bike. Each travel service have their estimate time & cost of travel. Traveler can run timely choose which service he/she can use while traveling.
#include<iostream>
using namespace std;
class Vehicle
{
protected:
int costperkm;
public:
virtual void Travel(int distance)=0;
};
class Cab : public Vehicle
{
public:
Cab()
{
costperkm = 10;
}
void Travel(int distance)
{
cout<<"Trave by Cab : ";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Bus : public Vehicle
{
public:
Bus()
{
costperkm = 5;
}
void Travel(int distance)
{
cout<<"Travle by Bus : ";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Train : public Vehicle
{
public:
Train()
{
costperkm = 2;
}
void Travel(int distance)
{
cout<<"Travel by Train :";
cout<<"Total cost "<<distance*costperkm<<endl;
}
};
class Travel
{
Vehicle *trans;
public:
void SetVehicle(Vehicle *v)
{
trans = v;
}
void TravelUser(int distance)
{
trans->Travel(distance);
}
};
int main()
{
Vehicle *cab = new Cab;
Travel t;
t.SetVehicle(cab);
t.TravelUser(20);
}

Comments
Post a Comment