锘??xml version="1.0" encoding="utf-8" standalone="yes"?>杞細浣滅敤:
灏嗕竴涓?#29831;?#23033;?#28751;?#29761;?#28051;轰竴涓?#28725;?#29854;?浠庤?#28003;?#28003;犲彲?#25956;涓?#37722;?#37928;勮姹傚瀹?#37812;疯繘琛屽弬鏁板寲;瀵?#29831;?#23033;傛帓闃?#37812;?#29825;板綍璇?#23033;傛棩?#32340;,浠ュ強鏀寔?#24434;?#25369;娑?#37928;?#37823;?#28003;?
UML缁?#37835;?#37733;?


瑙?#37835;?
Comnand妯?#23534;忕殑鎬濇兂鏄妸鍛?#28000;?#28751;?#29761;?#37734;ㄤ竴涓被涓?灏?#37828;?#26473;欓噷鐨凜ommand鍩虹被,鍚?#37827;舵妸?#24116;鏀?#28725;?#29854;?#28052;?#28751;?#29761;?#37734;ㄤ竴涓被涓?#28751;?#37828;?#26473;欓噷鐨凴eceiver?#34987;涓?鐢辫皟?#25956;杩?#28051;?#37723;?#28000;?#37928;勭被涔?#28751;?#37828;?#26473;欓噷鐨処nvoker?#34987;?#28533;?#30367;?#25956;.鍏?#28729;?濡?#37835;?#23534;勬竻妤氫簡Command妯?#23534;忕殑?#24107;鐞?灏变?#27692;?#25120;幇鍏?#28729;?#28729;冨拰娉ㄥ唽鍥炶皟鍑?#37825;?#37928;勫師鐞?#37828;?#23536;堢浉浼?#37928;?鑰屽湪闈?#37722;?#26473;囩▼鐨?#29825;?#29825;?#28051;?#37928;?#37733;炶皟鍑?#37825;板叾瀹炲拰杩欓噷鐨凜ommand?#34987;鐨?#28003;滅敤鏄竴鑷?#37928;?閲囩敤Command妯″紡瑙?#38000;︿簡鍛?#28000;?#37928;?#37721;?#37713;?#38000;呭拰鍛?#28000;?#37928;勬墽琛?#38000;?

瀹炵幇:
1)Command.h
/********************************************************************
    created:    2006/08/04
    filename:     Command.h
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    Command妯?#23534;忕殑?#32040;绀?#28000;?#37934;?br />********************************************************************
*/


#ifndef COMMAND_H
#define COMMAND_H

class Command
{
public:
    
virtual ~Command() {}

    
virtual void Execute() = 0;
}
;

class Receiver
{
public:
    
void Action();
}
;

class Invoker
{
public:
    Invoker(Command 
*pCommand);
    
~Invoker();

    
void Invoke();
private:
    Command 
*m_pCommand;
}
;

class ConcreateComand
    : 
public Command
{
public:
    ConcreateComand(Receiver
* pReceiver);
    
virtual ~ConcreateComand();

    
virtual void Execute();

private:
    Receiver
* m_pReceiver;
}
;

#endif

2)Command.cpp
/********************************************************************
    created:    2006/08/04
    filename:     Command.cpp
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    Command妯?#23534;忕殑?#32040;绀?#28000;?#37934;?br />********************************************************************
*/


#include 
"Command.h"
#include 
<iostream>

void Receiver::Action()
{
    std::cout 
<< "Receiver Action\n";
}


Invoker::Invoker(Command 
*pCommand)
    : m_pCommand(pCommand)
{

}


Invoker::
~Invoker()
{
    delete m_pCommand;
    m_pCommand 
= NULL;
}


void Invoker::Invoke()
{
    
if (NULL != m_pCommand)
    
{
        m_pCommand
->Execute();
    }

}


ConcreateComand::ConcreateComand(Receiver
* pReceiver)
    : m_pReceiver(pReceiver)
{

}


ConcreateComand::
~ConcreateComand()
{
    delete m_pReceiver;
    m_pReceiver 
= NULL;
}


void ConcreateComand::Execute()
{
    
if (NULL != m_pReceiver)
    
{
        m_pReceiver
->Action();
    }


    std::cout 
<< "Execute by ConcreateComand\n";
}


3)Main.cpp
/********************************************************************
    created:    2006/08/04
    filename:     main.cpp
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    Command妯?#23534;忕殑?#31092;璇?#28000;?#37934;?br />********************************************************************
*/


#include 
"Command.h"
#include 
<stdlib.h>

int main()
{
    Receiver
* pReceiver = new Receiver();
    Command
*  pCommand  = new ConcreateComand(pReceiver);
    Invoker
*  pInvoker  = new Invoker(pCommand);

    pInvoker
->Invoke();

    delete pInvoker;

    system(
"pause");

    
return 0;
}



灏忕帇 2011-08-05 01:50 鍙?#29723;?#29831;?#29825;?/a>
]]>甯歌璁?#29825;?#22959;?#23534;忕?#21230;В鏋愬拰瀹炵幇(C++)涔?#37719;?#27996;?ChainOfResponsibility妯″紡 http://www.2676208.com/jack-wang/archive/2011/08/05/152487.html灏忕帇灏忕帇Thu, 04 Aug 2011 17:46:00 GMThttp://www.2676208.com/jack-wang/archive/2011/08/05/152487.htmlhttp://www.2676208.com/jack-wang/comments/152487.htmlhttp://www.2676208.com/jack-wang/archive/2011/08/05/152487.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/152487.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/152487.html

浣滅敤:
浣垮涓?#28725;?#29854;?#38318;芥湁鏈?#28028;氬鐞?#29831;?#23033;?浠庤岄伩鍏?#29831;?#23033;?#37928;?#37721;戦佽呭拰?#24116;鏀?#38000;?#28052;?#38338;?#37928;勮?#37722;?#37711;崇郴.灏?#26473;?#27996;?#28725;?#29854;?#26473;?#37812;愪竴鏉¢摼锛屽苟娌跨潃杩?#37833;¢摼浼?#38315;?#29831;?#29831;?#23033;?鐩?#37714;?#37832;変竴涓?#28725;?#29854;?#28598;?#37918;?#28729;?#28051;烘.

UML缁?#37835;?#37733;?



鎶?#29854;?#37737;虹被:
1)Handler:瀹?#28052;変竴涓?#28598;?#37918;?#29831;?#23033;?#37928;勬帴鍙?鍦?#37733;?#28051;?#26473;?#28051;帴鍙?#28751;?#37828;疕andleRequset鍑?#37825;?杩?#28051;被鍚?#37827;?#37832;変竴涓寚鍚慔andler瀵?#29854;?#37928;勬寚閽??#23514;鍚?#37722;?#32513;?#37928;勫鐞?#29831;?#23033;?#37928;?#28725;?#29854;?濡?#37835;?#37832;?#37928;勮瘽).

瑙?#37835;?
杩?#28051;?#22959;″紡?#22968;?#24434;浠?#28598;?#37918;嗕竴涓?#29831;?#23033;?#37928;?#28725;?#29854;?#28000;ラ摼鐨勫舰?#32033;杩?#37734;?#27996;嗕竴璧?璁?#26473;?#27996;?#28725;?#29854;?#38318;芥湁澶?#37918;?#29831;?#23033;?#37928;勬?#36720;細.濂芥瘮鍘熸潵鐪嬪彜瑁?#37922;?#29785;嗕腑缁?#29999;?#37930;?#37714;?#37928;?#28729;?#28051;?#37721;?#29785;佹煇浜?#37928;?#37827;跺?澶?#37929;?#28000;??#24434;浠?#28598;?#37918;嗕竴涓?#29831;?#23033;?#37928;?#28725;?#29854;?灏变細渚濇鐨勫枈:浼燲X...杩欐牱?#31476;鐩?#28051;嬪?#33636;洿鍒版壘鍒?#26473;?#28051;?#27996;?#28051;烘.ChainOfResponsibility妯″紡涔熸槸杩欐牱鐨勫鐞?#29831;?#23033;?#37928;?濡?#37835;?#37832;?#37722;?#32513;?#37928;?#28725;?#29854;″彲浠?#28598;?#37918;?閭?#28052;?#28028;?#32513;?#37722;?#32513;?#37928;?#28725;?#29854;?#28598;?#37918;?鍚?#37714;?#28751;辫嚜宸?#28598;?#37918;?#29831;?#23033;?杩欐牱鐨?#29825;?#29825;℃妸璇?#23033;?#37928;?#37721;戦佽呭拰璇?#23033;?#26473;欑鐨勫鐞?#38000;?#29785;?#38000;︿簡,濂芥瘮鍙?#37721;?#37928;?#37928;?#29999;?#28051;?#37933;ラ亾鍒板簳鏄摢涓?#28598;?#37929;?#37832;鍚庝細?#22744;鍒?#28000;栬?#20345;壘鍒?#37928;?#27996;轰竴鑸?鍙鍙?#37713;?#37723;?#28000;?#28751;監K?#31777;.

瀹炵幇:
1)ChainOfResponsibility.h
/********************************************************************
    created:    2006/07/20
    filename:     ChainOfResponsibility.h
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    ChainOfResponsibility妯?#23534;忕殑?#32040;绀?#28000;?#37934;?br />********************************************************************
*/


#ifndef CHAINOFRESPONSIBILITY_H
#define CHAINOFRESPONSIBILITY_H

#include 
<stdio.h>

// 鎶?#29854;?#37737;虹被,瀹?#28052;変竴涓?#28598;?#37918;?#29831;?#23033;?#37928;勬帴鍙?/span>
class Handler
{
public:
    Handler(Handler 
*pSuccessor = NULL);
    
virtual ~Handler();

    
// 绾?#27692;嚱鏁?鐢辨淳鐢熺被瀹炵幇
    virtual void HandleRequset() = 0;

protected:
    Handler
* m_pSuccessor;
}
;

class ConcreateHandler1
    : 
public Handler
{
public:
    ConcreateHandler1(Handler 
*pSuccessor = NULL) : Handler(pSuccessor){}
    
virtual ~ConcreateHandler1(){}

    
virtual void HandleRequset();
}
;

class ConcreateHandler2
    : 
public Handler
{
public:
    ConcreateHandler2(Handler 
*pSuccessor = NULL) : Handler(pSuccessor){}
    
virtual ~ConcreateHandler2(){}

    
virtual void HandleRequset();
}
;

#endif

2)ChainOfResponsibility.cpp
/********************************************************************
    created:    2006/07/20
    filename:     ChainOfResponsibility.cpp
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    ChainOfResponsibility妯?#23534;忕殑?#32040;绀?#28000;?#37934;?br />********************************************************************
*/


#include 
"ChainOfResponsibility.h"
#include 
<iostream>

Handler::Handler(Handler 
*pSuccessor /* = NULL*/
    : m_pSuccessor(pSuccessor)
{
}


Handler::
~Handler()
{
    delete m_pSuccessor;
    m_pSuccessor 
= NULL;
}


void ConcreateHandler1::HandleRequset()
{
    
if (NULL != m_pSuccessor)
    
{
        m_pSuccessor
->HandleRequset();
    }

    
else
    
{
        std::cout 
<< "HandleRequset by ConcreateHandler1\n";
    }

}


void ConcreateHandler2::HandleRequset()
{
    
if (NULL != m_pSuccessor)
    
{
        m_pSuccessor
->HandleRequset();
    }

    
else
    
{
        std::cout 
<< "HandleRequset by ConcreateHandler2\n";
    }

}



3)Main.cpp
/********************************************************************
    created:    2006/07/20
    filename:     Main.cpp
    author:        鏉?#37714;?br />                
http://www.2676208.com/converse/

    purpose:    ChainOfResponsibility妯?#23534;忕殑?#31092;璇?#28000;?#37934;?br />********************************************************************
*/


#include 
"ChainOfResponsibility.h"
#include 
<stdlib.h>

int main()
{
    Handler 
*p1 = new ConcreateHandler1();
    Handler 
*p2 = new ConcreateHandler2(p1);

    p2
->HandleRequset();

    delete p2;

    system(
"pause");

    
return 0;
}



灏忕帇 2011-08-05 01:46 鍙?#29723;?#29831;?#29825;?/a>
]]>Command妯″紡http://www.2676208.com/jack-wang/archive/2010/01/16/105820.html灏忕帇灏忕帇Sat, 16 Jan 2010 08:27:00 GMThttp://www.2676208.com/jack-wang/archive/2010/01/16/105820.htmlhttp://www.2676208.com/jack-wang/comments/105820.htmlhttp://www.2676208.com/jack-wang/archive/2010/01/16/105820.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/105820.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/105820.html

灏忕帇 2010-01-16 16:27 鍙?#29723;?#29831;?#29825;?/a>
]]>Adapter妯″紡http://www.2676208.com/jack-wang/archive/2010/01/16/105811.html灏忕帇灏忕帇Sat, 16 Jan 2010 06:42:00 GMThttp://www.2676208.com/jack-wang/archive/2010/01/16/105811.htmlhttp://www.2676208.com/jack-wang/comments/105811.htmlhttp://www.2676208.com/jack-wang/archive/2010/01/16/105811.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/105811.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/105811.html


灏忕帇 2010-01-16 14:42 鍙?#29723;?#29831;?#29825;?/a>
]]>Facade妯″紡http://www.2676208.com/jack-wang/archive/2010/01/16/105810.html灏忕帇灏忕帇Sat, 16 Jan 2010 06:32:00 GMThttp://www.2676208.com/jack-wang/archive/2010/01/16/105810.htmlhttp://www.2676208.com/jack-wang/comments/105810.htmlhttp://www.2676208.com/jack-wang/archive/2010/01/16/105810.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/105810.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/105810.html


浣跨敤Facade妯″紡鍚?#37928;勭?#23338;?#24533;粨鏋勶細



灏忕帇 2010-01-16 14:32 鍙?#29723;?#29831;?#29825;?/a>
]]>鎶?#29854;?#23480;?#37720;?Abstract Factory) http://www.2676208.com/jack-wang/archive/2009/01/02/70971.html灏忕帇灏忕帇Thu, 01 Jan 2009 20:23:00 GMThttp://www.2676208.com/jack-wang/archive/2009/01/02/70971.htmlhttp://www.2676208.com/jack-wang/comments/70971.htmlhttp://www.2676208.com/jack-wang/archive/2009/01/02/70971.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/70971.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/70971.html闃?#29831;?#37711;?#37826;?/a>

灏忕帇 2009-01-02 04:23 鍙?#29723;?#29831;?#29825;?/a>
]]>宸?#37720;傛柟娉?#38171;坒actory-method锛?#22959;″紡http://www.2676208.com/jack-wang/archive/2009/01/02/70970.html灏忕帇灏忕帇Thu, 01 Jan 2009 20:22:00 GMThttp://www.2676208.com/jack-wang/archive/2009/01/02/70970.htmlhttp://www.2676208.com/jack-wang/comments/70970.htmlhttp://www.2676208.com/jack-wang/archive/2009/01/02/70970.html#Feedback0http://www.2676208.com/jack-wang/comments/commentRss/70970.htmlhttp://www.2676208.com/jack-wang/services/trackbacks/70970.html?#31476;锛?span> 宸?#37720;傛柟娉?#38171;坒actory-method锛?#22959;″紡

 

1锛?span> 鎰?#37733;?/span>

瀹?#28052;変竴涓?#37714;涘缓瀵?#29854;?#37928;勬帴鍙?#38171;?#29825;╁瓙?#34987;鍐?#28729;氬疄渚嬪寲?#25698;涓?#27996;?#37725;佺被銆傚伐鍘傛柟娉?#28003;夸竴涓?#28725;?#29854;?#37928;?#37714;涘缓寤?#26473;?#37714;板瓙?#34987;銆?/span>

 

2锛?span> 閫傜敤鎬?/span>

褰撲竴涓被涓?#37933;ラ亾瀹?#29781;?#37714;涘缓鐨?#28725;?#29854;?#37928;勭被鏃?#37510;?/span>

褰撲竴涓被甯屾湜鐢?#28729;?#37928;勫瓙?#34987;?#28533;鍐?#28729;氬垱寤哄摢涓被鐨?#28725;?#29854;?#37827;?#37510;?/span>

褰撲竴涓被鍐?#28729;氬皢鍒涘缓瀵?#29854;?#28654;?#37813;?#32513;?#28598;?#28051;瓙?#34987;锛屽苟涓?#29999;屾湜灏?#38317;?#28051;瓙?#34987;鏄?#28000;?#37918;?#26473;欎竴淇℃伅灞閮ㄥ寲鐨?#37827;跺?#37510;?/span>

 

3锛?span> 缁?#37835;?/span>


4锛?span> ?#24364;涓庤?/span>

l         Creater?#32048;

澹?#37828;庡伐鍘傛柟娉?#37928;勬娊璞$被锛屽伐鍘傛柟娉?#26473;?#37733;炰竴涓?#27996;?#37725;佺被锛?/span>Product锛?#37510;?/span>

?#24434;浠ヨ皟?#25956;宸?#37720;傛柟娉?#26473;?#37733;炰竴涓?#27996;?#37725;?#28725;?#29854;?#37510;?/span>

 

l         ConcreteCreater?#32048;

閲?#28729;?#28052;夊伐鍘傛柟娉?#38171;?#28000;?#26473;?#37733;炰竴涓壒瀹?#37928;?/span>product銆?/span>

 

l         Product:

浜?#37725;?#37814;?#29854;$被锛岀粰?#25956;鎴锋?#24874;?#28055;竴鑷?#37928;?#27996;?#37725;佹帴鍙?#37510;?/span>

 

l         ConcreteProduct:

?#22738;瀹?#37928;?#27996;?#37725;?#38171;?#28729;炵幇product?#24116;鍙?#37510;?/span>

 

5锛?span> 鏁?#37835;?/span>

          宸?#37720;傛柟娉?#28751;嗙壒瀹?#37928;勭被鐨?#28729;?#28186;嬪寲寤?#26473;?#37714;板瓙?#34987;锛?#38000;?#28051;?#26473;?#37733;?/span>product?#24116;鍙?#38171;?#28003;跨敤鎴?#28000;?#37934;?#28051;嶇敤?#25328;鍏?#28003;撶被鎵?#27996;ら亾锛屼竴鑷?#37928;?#28725;瑰緟Product銆傛墍浠?#37734;ㄧ敤鎴疯?#20344;?#29362;叆鏂?#27996;?#37725;?#37928;?#37827;跺?#38171;?#37827;犻渶?#27967;鏀?#28729;?#37812;?#28000;?#37934;?#38171;屽彧?#28214;鍔犲叆?#31476;涓?#37826;?#37928;?/span>ConcreteProduct?#25328;ConcreteCreater鍗冲彲锛?#26473;?#28051;?#28052;熸槸閬?/span> 瀹堜簡“寮鏀?#28751;?#38338;師鍒?#8221;銆?/span>

                                    杩炴帴骞?#29723;?#37928;勭被?#30512;嬈?#38171;?#28051;?#38344;?#37928;?#32513;?#37835;?#37733;?#28051;?#38171;?/span>ConcreteCreater1瀵瑰簲鍒?/span>ConcreterProduct1鑰?/span>ConcreteCreater2瀵瑰簲鍒涘缓ConcreterProduct2銆傛墍浠?/span>creater?#25328;product鐨勭被?#30512;嬈?#37828;?#39582;?#29723;?#37928;?#38171;岀敤鎴?#37721;渶閫?#28729;氫竴涓?/span>Creater锛?#28751;?#37714;涘缓鍑?#28725;瑰簲product銆傚儚鏄竴涓?/span>map?#31476;?#29297;锛?#28051;嶄?#27692;?#34425;幇鐗涘ご涓?#28725;?#26937;槾涔?#37804;?#37510;?/span>

 

6锛?span> 瀹炵幇?#24375;鍙?#28003;?/span>

                                Creater鏈?#28051;?#32457;?#28729;炵幇锛屼竴绉?#28751;?#37828;?#37814;?#29854;?#37928;勫伐鍘傛柟娉?#38171;屽叿浣?#28729;炵幇?#26240;鍒板瓙?#34987;銆傚彟?#31476;绉?#28751;?#37828;?#37832;変竴涓?#34425;渷鐨?#28729;炵幇锛屽瓙?#34987;涔熷彲浠?#38322;?#37826;?#28729;炵幇杩?#28051;?#37826;?#23049;?#37510;?#26473;欑鏂?#23049;?#28003;跨敤浜庣湡鐨勬?#22793;竴涓粯璁?#37928;?/span>Product?#28214;瑕?#28729;?#28186;嬪寲鐨勮繖绉嶆?#21613;喌銆?/span>

       甯﹀弬鏁?#37928;勫伐鍘傛柟娉?#38171;?#26473;欑?#23942;?#21613;?#38747;彲浠?#37714;涘缓澶氱?#23940;骇鍝?#38171;?#28051;?#26473;?#37832;?#28051;?#38340;?#37714;?#28751;?#37828;?#37813;鏈?#27996;?#37725;侀兘瑕?#28729;炵幇product?#24116;鍙?#38171;?#37722;?#37714;?#28751;?#28598;?#37720;诲伐鍘傛柟娉?#37928;勬剰涔変簡銆?#28000;?#37934;?#28641;?#28051;嬶細

Product* ConcreteCreater1::FactoryMethod(int nID)

{

     If(nID == BUTTON)

     {

         return new button;

}

Else if(nID == BOX)

{

    return new box;

}

}

 

?#25956;妯?#37833;?#28729;炵幇锛岀?#20346;暐鍒涘缓?#29913;?#34987;銆?/span>

 class Creater

{

public:

     virtual Product* FactoryMethod() = 0;

};

 

template<class theProduct>

class TempCreater: public Creater

{

     virtual Product* FactoryMethod();

};

 

 

template<class theProduct>

Product* TempCreater<theProduct>::FactoryMethod()

{

     return new theProduct;

}

        

         浣跨敤杩?#28051;?#22959;?#37911;?#28729;?#37812;?#32468;?#37721;渶瀹?#28052;?#27996;?#37725;?#38171;?#38000;?#28051;嶉渶瀹?#28052;?/span>creater鐨勫瓙?#34987;銆?/span>

 

浜?#38171;?span> 绋嬪簭涓句緥

        鍦ㄤ竴涓?/span>ACT?#29238;鎴?#28051;?#38171;?#28051;?#29785;?#26473;?#37711;?#26473;囩▼涓?#37832;?#29825;?#28598;?#37825;屼汉锛屽?#20636;嫾锛?/span>wolf锛?/span>,铦?#38118;?#38171;?/span>bat锛?/span>,鑰佹?#38171;?/span>BOSS锛?/span>,鍦?#23011;忎竴鍏?#37928;?#37714;氬紑濮嬪氨瑕?#37714;涘缓璁?#28598;?#28051;?#37722;?#37928;?#37825;屼汉锛屽鏋?#28051;嶇敤宸?#37720;傛柟娉?#38171;?#37714;欐妸鎵鏈?#37928;?#37714;涘缓浠诲姟?#20824;鏀?#37734;ㄤ簡瀹?#37812;?#28000;?#37934;?#28051;?#38171;?#37714;欎竴涓?#28000;?#37934;?#28051;?#37828;撴墿鍏呭拰淇?#37824;?#38171;屼簩鏄?#28000;?#37934;?#23536;?#37833;?#28052;?#37510;?#38000;岀敤?#31777;宸?#37720;傛柟娉?#37714;欏彲浠?#37711;?#37832;?#26473;?#28051;?#28051;?#34425;偣銆傛瘮濡?#28003;犲?#28850;姞?#31476;涓?#37826;?#37928;?#37825;屼汉-鎭愰緳锛?#37714;?#37721;渶澧炲姞?#31476;涓?#37805;愰?#27409;被锛?/span>ConcreteProduct锛?#38171;屽?#23941;?#28850;姞?#31476;涓?#37714;涘?#28888;?#24880;緳鐨勭被锛?/span>ConcreteCreater 锛?#28751;?/span>OK?#31777;銆?br>

婧?#37934;?#28641;?#28051;嬶細

 

//product abstract class

class Enemy
{
public:
    
virtual void attack() = 0;
    
virtual void Draw() = 0;
    
virtual ~Enemy() = 0;
}
;

// concrete product class
class Wolf : public Enemy
{
    
void attack()
    
{
        cout 
<< "Wolf attack me!" << endl;
    }

    
void Draw()
    
{
        cout 
<< "I am Wolf!" << endl;
    }

}
;

// concrete product class
class Bat : public Enemy
{
    
void attack()
    
{
        cout 
<< "Bat attack me!" << endl;
    }

    
void Draw()
    
{
        cout 
<< "I am Bat!" << endl;
    }

}
;

// concrete product class
class Boss : public Enemy
{
    
void attack()
    
{
        cout 
<< "Boss attack me!" << endl;
    }

    
void Draw()
    
{
        cout 
<< "I am Boss!" << endl;
    }

}
;

// creater abstract class
class EnemyCreater
{
    
virtual Enemy* CreateEnemy() = 0;
}
;

// concrete creater class
class WolfCreater : public EnemyCreater
{
     Enemy
* CreateEnemy()
     
{
         
return new Wolf;
     }

}
;

// concrete creater class
class BatCreater : public EnemyCreater
{
     Enemy
* CreateEnemy()
     
{
         
return new Bat;
     }

}
;

// concrete creater class
class BossCreater : public EnemyCreater
{
     Enemy
* CreateEnemy()
     
{
         
return new Boss;
     }

}
;


int main(int argc, char* argv[])
{
    EnemyCreater
* enemyCreater[3];
    enemyCreater[
0= new WolfCreater;
    enemyCreater[
1= new BatCreater;
    enemyCreater[
2= new BossCreater;
    Enemy
* cruEnemy = NULL;
    
for(int i  = 0; i < 3++i)
    
{
        cruEnemy 
= enemyCreater[i].CreateEnemy();
        
        cruEnemy
->Draw();
        cruEnemy
->attack();

        delete cruEnemy;
    }

    
return 0;
}
 


 

涓?#38171;?span> ?#27977;鍏?#22959;″紡

 Abstract Factory缁?#29999;哥敤宸?#37720;傛柟娉曟潵瀹炵幇銆?/span>

 宸?#37720;傛柟娉?#38315;氬父鍦?/span>Template Methods涓?#30367;?#25956;銆?/span>

    Prototypes涓嶉渶瑕?#37714;涘缓Creater鐨勫瓙?#34987;銆?#28003;?#37828;?#38171;?#28729;?#28000;?#38315;氬?#27468;姹備竴涓?#38333;?#28725;?/span>Product?#34987;鐨?/span>Initialize鎿?#28003;?#37510;?/span>Creater浣跨敤Initialize?#28533;鍒?#28654;嬪寲瀵?#29854;?#37510;?#38000;?/span>Factory Method涓嶉渶瑕佽繖?#29297;鐨?#37823;?#28003;?#37510;?/span>

                                                                                                                                                     

 

鍥?#38171;?span> ?#24364;鑰?#37826;囩尞

 

 銆?font size=3>璁?#29825;?#22959;?#23534;忕?#25424;В銆?/font> ?#31483;鍗庡ぇ瀛?#37713;虹?#22562;ぞ锛?/span> 鐔婃澃璇?#37510;?/span>

  銆?#29825;?#29825;?#22959;″紡?#24434;澶嶇敤闈?#37722;?#28725;?#29854;?#26462;?#28000;?#37928;?#37737;虹銆?/span> 鏈烘?#26495;伐涓氬?#34425;?#22562;ぞ锛?/span> 鍥?#27996;?#37733;㈣憲銆?/span>



灏忕帇 2009-01-02 04:22 鍙?#29723;?#29831;?#29825;?/a>
]]>
探灵笔记系统攻略
极速时时结果 北京幸运28 2分彩骗局 7彩app正规吗 赛车pk拾开奖 湖北11选5计划软件 8号彩票网络地址 真钱21点可以赚真钱的手机游戏 北京时时两期计划 新强时时彩最新开奖结果走势图