排兵布陣之interpreter篇
時間:2024-10-20 來源: 作者: 我要糾錯
<p style=line-height: 150%>接上回:<p style=line-height: 150%><A >http://www.csdn***/develop/read_article.asp?id=11439</A><p style=line-height: 150%>排兵布陣時使用interpreter模式,
可以讓教練不用去hack,而是簡單
的復(fù)用就可以完成復(fù)雜的戰(zhàn)術(shù)。
在這個模式中,client是教練,
context是球隊,abstractexpression
是基本打法,terminalexpression
是單個隊員的打法,nonterminal
expression是一條線的打法,
好處是用類來表示打法,可以用
繼承來改變或擴展打法;而且
"抽象打法樹"中各節(jié)點的類的
實現(xiàn)大體類似,易于實現(xiàn)。缺點是
為每種打法定義一個類,當(dāng)打法
很復(fù)雜時,很難維護。interpreter
和composite(組合進(jìn)攻)在實現(xiàn)上
有很多相通之處,以下是需要考慮的
特殊問題:
1.抽象打法樹的創(chuàng)建??梢允褂媒?jīng)典
教科書上的打法,也可以由教練提供。
2.打法的實現(xiàn)可以采用visitor(全攻全守)
來實現(xiàn)。
3.當(dāng)許多打法都以某個隊員作為
最終完成者時,這個隊員可以作為
flyweight來共享:)
代碼如下:
class 基本打法{
public:
基本打法();
virtual ~基本打法();<p style=line-height: 150%> virtual bool 組織(球隊&)=0;
virtual 基本打法* 進(jìn)攻(const char*,基本打法&)=0;
virtual 基本打法* 防守(const char*,基本打法&)=0;
};<p style=line-height: 150%>class 球隊{
public:
bool 找尋球員(const char*) const;
void 賦給(球員打法*,bool);
};<p style=line-height: 150%>class 左后衛(wèi)打法:public 基本打法{
public:
左后衛(wèi)打法(const char*);
virtual ~左后衛(wèi)打法();<p style=line-height: 150%> virtual bool 組織(球隊&);
virtual 基本打法* 進(jìn)攻(const char*,基本打法&);
virtual 基本打法* 防守(const char*,基本打法&);
private:
char* _左后衛(wèi)號碼;
};<p style=line-height: 150%>class 雙前鋒打法:public 基本打法{
public:
雙前鋒打法(基本打法*,基本打法*);
virtual ~雙前鋒打法();<p style=line-height: 150%> virtual bool 組織(球隊&);
virtual 基本打法* 進(jìn)攻(const char*,基本打法&);
virtual 基本打法* 防守(const char*,基本打法&);
private:
基本打法* _前鋒甲;
基本打法* _前鋒乙;
};
標(biāo)簽: