// Strategy. cpp: defines the entry point of the console application.
//
# Include "stdafx. H"
# Include <iostream>
Using namespace STD;
Class strategy;
Class Context
{
Public:
Context (Strategy * pstrategy = NULL): m_pstrategy (pstrategy)
{
}
~ Context ()
{
}
Void contextinterface ();
Void setstrategy (Strategy * s)
{
M_pstrategy = s;
}
PRIVATE:
Strategy * m_pstrategy;
};
Class strategy
{
Public:
Virtual ~ Strategy (){}
Virtual void algorithminterface () = 0;
};
Class concreatestrategya
: Public strategy
{
Public:
Virtual ~ Concreatestrategya (){}
Virtual void algorithminterface ()
{
Cout <"algorithminterface implemented by concreatestrategya/N ";
}
};
Class concreatestrategyb
: Public strategy
{
Public:
Virtual ~ Concreatestrategyb (){}
Virtual void algorithminterface ()
{
Cout <"algorithminterface implemented by concreatestrategyb/N ";
}
};
Void context: contextinterface ()
{
If (null! = M_pstrategy)
{
M_pstrategy-> algorithminterface ();
}
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Strategy * pstrategya = new concreatestrategya ();
Context * pcontext = new context (pstrategya );
Pcontext-> contextinterface ();
Cout <"=============================" <Endl;
Strategy * pstrategyb = new concreatestrategyb ();
Pcontext-> setstrategy (pstrategyb );
Pcontext-> contextinterface ();
Delete pstrategya;
Delete pstrategyb;
Delete pcontext;
Return 0;
}