// Strategy. cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
/*************************************** *****************************
Created: 2011/08/15
Created: PM
File base: strategy
File ext: cpp
Author: lost boy
Purpose: Design Mode-policy Mode
Policy mode refers to defining a series of algorithms, encapsulating them one by one, and making them replaceable.
This mode allows algorithms to change independently of customers who use it. That is to say, these algorithms provide the same functions,
External interfaces are the same, but their implementations are different. It is better to encapsulate algorithms in policy mode.
**************************************** *****************************/
/*
Computer Tactics
*/
Class BotsTactics
{
Public:
Virtual void DoTactics () = 0;
};
/*
Blow stream
*/
Class TacticsBlown: public BotsTactics
{
Public:
Void DoTactics ()
{
Printf ("bot use Blown tactics \ n ");
}
};
/*
Bear stream
*/
Class TacticsBears: public BotsTactics
{
Public:
Void DoTactics ()
{
Printf ("bot use Bears tactics \ n ");
}
};
/*
Streaming
*/
Class TacticsTowerRush: public BotsTactics
{
Public:
Void DoTactics ()
{
Printf ("bot use TowerRush tactics \ n ");
}
};
/*
It is also directly specified through the parameter, but is not a pointer, but a tag.
You only need to know the tag
*/
Enum TATICS {BLOWN, BEAR, TR}; // tag
Class Bots
{
Private:
BotsTactics * pTatics _;
Public:
Bots (enum TATICS ta)
{
If (ta = BLOWN)
PTatics _ = new TacticsBlown;
Else if (ta = BEAR)
PTatics _ = new TacticsBears;
Else if (ta = TR)
PTatics _ = new TacticsTowerRush;
Else
PTatics _ = NULL;
}
~ Bots ()
{
If (pTatics _)
{
Delete pTatics _;
PTatics _ = NULL;
}
};
Public:
Void DoTactics ()
{
PTatics _-> DoTactics ();
}
};
/*
Use templates. The algorithm is specified by the real parameters of the template.
*/
Template <class ta>
Class templateBots
{
Private:
Ta _;
Public:
TemplateBots (){}
~ TemplateBots (){}
Public:
Void DoTactics ()
{
Ta _. DoTactics ();
}
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
Bots bot (BLOWN );
Bot. DoTactics ();
TemplateBots <TacticsTowerRush> bot1;
Bot1.DoTactics ();
Return 0;
}