Most of the time after the has been developed. We will be asked for adding new features for the existing software. In such cases, we have to spend more time to change the previously written codes and to change some logic as well. This will be a more time consuming process rather than designing a new software. We all know that object oriented designs are re-usable. But when we are designing something we have to put more concentration. Because our design should capable of catering newly coming requirements as well. If there is a possibility to change the behaviors at run-time we can use strategy pattern.
An example for Strategy Pattern
Lets say, we have a vehicle factory which produces different types of vehicles. Company is currently producing cars with patrol engine behavior and diesel engine behavior. Imagine they are looking forward to produce cars with some other engine behaviors as well. Let's say they need to produce the same type of a car with some other engine. This will be a huge problem if the behavior of engine is bound to the vehicle itself as an abstract function. Solution is as follows,
Vehicle.h
#ifndef VEHICLE_H
#define VEHICLE_H
#include "Engine.h"
class Vehicle
{
public:
enum Type
{
CAR,
VAN,
UNDEFINED
};
Vehicle();
virtual ~Vehicle();
Engine* getEngine() const;
virtual void setEngine(Engine* val);
void startEngine();
virtual Type getType();
protected:
Type type_;
private:
Engine* engine_;
};
#endif //VEHICLE_H
#define VEHICLE_H
#include "Engine.h"
class Vehicle
{
public:
enum Type
{
CAR,
VAN,
UNDEFINED
};
Vehicle();
virtual ~Vehicle();
Engine* getEngine() const;
virtual void setEngine(Engine* val);
void startEngine();
virtual Type getType();
protected:
Type type_;
private:
Engine* engine_;
};
#endif //VEHICLE_H
Vehicle.cpp
#include "Vehicle.h"
#include
#include
Vehicle::Vehicle() : type_(UNDEFINED), engine_(NULL)
{
}
Engine* Vehicle::getEngine() const
{
return engine_;
}
Vehicle::~Vehicle()
{
delete engine_;
}
void Vehicle::setEngine( Engine* val )
{
engine_ = val;
}
void Vehicle::startEngine()
{
_ASSERT(engine_ != NULL);
engine_->start();
}
Vehicle::Type Vehicle::getType()
{
return type_;
}
#include
#include
Vehicle::Vehicle() : type_(UNDEFINED), engine_(NULL)
{
}
Engine* Vehicle::getEngine() const
{
return engine_;
}
Vehicle::~Vehicle()
{
delete engine_;
}
void Vehicle::setEngine( Engine* val )
{
engine_ = val;
}
void Vehicle::startEngine()
{
_ASSERT(engine_ != NULL);
engine_->start();
}
Vehicle::Type Vehicle::getType()
{
return type_;
}
Car.h
#ifndef CAR_H
#define CAR_H
#include "Vehicle.h"
class Car : public Vehicle
{
public:
Car();
virtual ~Car();
Vehicle::Type getType();
private:
};
#endif //CAR_H
#define CAR_H
#include "Vehicle.h"
class Car : public Vehicle
{
public:
Car();
virtual ~Car();
Vehicle::Type getType();
private:
};
#endif //CAR_H
Car.cpp
#include "Car.h"
Car::Car()
{
type_ = Vehicle::CAR;
}
Car::~Car()
{
}
Vehicle::Type Car::getType()
{
return type_;
}
Car::Car()
{
type_ = Vehicle::CAR;
}
Car::~Car()
{
}
Vehicle::Type Car::getType()
{
return type_;
}
Engine.h
#ifndef ENGINE_H
#define ENGINE_H
class Engine
{
public:
Engine(){}
virtual void start()=0;
virtual ~Engine(){}
private:
};
#endif //ENGINE_H
#define ENGINE_H
class Engine
{
public:
Engine(){}
virtual void start()=0;
virtual ~Engine(){}
private:
};
#endif //ENGINE_H
DieselEngine.h
#ifndef DIESEL_ENGINE_H
#define DIESEL_ENGINE_H
#include "Engine.h"
class DieselEngine : public Engine
{
public:
DieselEngine();
~DieselEngine();
void start();
};
#endif //DIESEL_ENGINE_H
#define DIESEL_ENGINE_H
#include "Engine.h"
class DieselEngine : public Engine
{
public:
DieselEngine();
~DieselEngine();
void start();
};
#endif //DIESEL_ENGINE_H
DieselEngine.cpp
#include "DieselEngine.h"
#include << iostream >>
#include << iostream >>
DieselEngine::DieselEngine()
{
}
DieselEngine::~DieselEngine()
{
}
void DieselEngine::start()
{
std::cout<<"Starting Diesel engine ....";
}
PatrolEngine.h
#ifndef PATROL_ENGINE_H
#define PATROL_ENGINE_H
#include "Engine.h"
class PatrolEngine : public Engine
{
public:
PatrolEngine();
virtual ~PatrolEngine();
void start();
};
#endif //PATROL_ENGINE_H
#define PATROL_ENGINE_H
#include "Engine.h"
class PatrolEngine : public Engine
{
public:
PatrolEngine();
virtual ~PatrolEngine();
void start();
};
#endif //PATROL_ENGINE_H
PatrolEngine.cpp
#include "PatrolEngine.h"
#include << iostream >>
PatrolEngine::PatrolEngine()
{
}
PatrolEngine::~PatrolEngine()
{
}
void PatrolEngine::start()
{
std::cout<<"Starting Patrol engine ....";
}
#include << iostream >>
PatrolEngine::PatrolEngine()
{
}
PatrolEngine::~PatrolEngine()
{
}
void PatrolEngine::start()
{
std::cout<<"Starting Patrol engine ....";
}
main.cpp
Vehicle* v1 = new Car();
v1->setEngine(new DieselEngine());
v1->setEngine(new DieselEngine());
v1->startEngine();
Vehicle* v2 = new Car();
v2->setEngine(new PatrolEngine());
v2->startEngine();
v2->setEngine(new PatrolEngine());
v2->startEngine();
Output :
Starting Diesel engine ....
Starting Patrol engine ....
Download Visual Studio 2008 Solution : Strategy Pattern Sample in C++