Design Mode-policy Mode

Source: Internet
Author: User

// 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;
}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.