Abstract policy role: A policy class, usually implemented by an interface or abstract class.
-Specific policy role: wrapping the relevant algorithms and behaviors.
-Environment role: holds a reference to a policy class that is eventually called to the client.
#pragma once
Policy interface (pure virtual function)
Class IStrategy
{
Public
IStrategy () {}
Virtual ~istrategy () {}
virtual void Operate () = 0;
};
#pragma once
#include "IStrategy.h"
Policy 1, inheriting from the abstract base class
Class OneWay:p ublic istrategy
{
Public
OneWay (void);
~oneway (void);
void Operate (void);
};
#include "StdAfx.h"
#include "OneWay.h"
#include <iostream>
constructor function
Oneway::oneway (void)
{
}
Destructors
Oneway::~oneway (void)
{
}
Strategy 1 Specific implementation
void Oneway::operate (void)
{
Std::cout << "Find Qiaoguo old help, let Wu too to Sun Quan pressure" << Std::endl;
}
#pragma once
#include "IStrategy.h"
Policy 2, inheriting from the abstract base class
Class TwoWay:p ublic istrategy
{
Public
TwoWay (void);
~twoway (void);
void Operate (void);
};
#include "stdafx.h"
#include "TwoWay.h"
#include <iostream>
#include "string"
constructor function
Twoway::twoway (void)
{
}
Destructors
Twoway::~twoway (void)
{
}
Specific implementation of strategy 2
void Twoway::operate (void)
{
Std::cout << "Ask Wu to open a green light, release" << Std::endl;
}
#pragma once
#include "IStrategy.h"
Policy 3, inheriting from the abstract base class
Class Threeway:p ublic istrategy
{
Public
Threeway (void);
~threeway (void);
void Operate (void);
};
#include "stdafx.h"
#include "ThreeWay.h"
#include <iostream>
constructor function
Threeway::threeway (void)
{
}
Destructors
Threeway::~threeway (void)
{
}
Specific implementation of strategy 3
void Threeway::operate ()
{
Std::cout << "neighbour, block the pursuers" << Std::endl;
}
#pragma once
#include "IStrategy.h"
Kam SAC
Class Ccontext
{
Public
constructor function
Ccontext (IStrategy *pstrategy);
Destructors
~ccontext (void);
Behavior
void Operate (void);
Private
IStrategy * M_PSTRATEGY;
};
#include "stdafx.h"
#include "Bag.h"
constructor function
Ccontext::ccontext (IStrategy *pstrategy)
{
This->m_pstrategy = Pstrategy;
}
Destructors
Ccontext::~ccontext ()
{
Delete this->m_pstrategy;
}
Behavior
void Ccontext::operate ()
{
M_pstrategy->operate ();
}
#include "stdafx.h"
#include "Bag.h"
#include "OneWay.h"
#include "TwoWay.h"
#include "ThreeWay.h"
#include <iostream>
int _tmain (int argc, _tchar* argv[])
{
Ccontext *pcontext;
Std::cout << "----------just came to Wu when the first----------" << Std::endl;
PContext = new Ccontext (new OneWay ());
Pcontext->operate ();
Delete PContext;
Std::cout << "----------Liu Bei took a second----------" << Std::endl;
PContext = new Ccontext (new TwoWay ());
Pcontext->operate ();
Delete PContext;
Std::cout << "----------of Sun Quan's creeps. Dismantle the third----------"<< Std::endl;
PContext = new Ccontext (new Threeway ());
Pcontext->operate ();
Delete PContext;
return 0;
}
015 Strategy mode of design pattern