1
我實現了一個撲克手歷史文件解析器(PHP或Java代碼)的策略模式。我有一個問題來爲幾個策略使用的類和對象創建一個乾淨的OOP方法。如何將外部類用於策略模式?
在現實生活中:
我有一個類遊戲,其中有手(S)
class Hand
{
... // attributes for all strategies
... // (in fact : attributes that define the "core" of a Hand)
}
class Game
{
Hand hands[];
}
一種策略將創建一個對象,遊戲和手
class StrategyA implements IStrategy // strategy pattern
{
Game game;
function Parse()
{
game = new Game();
...
}
}
而這個策略將需要特定屬性的遊戲和每隻手 我不能把這些屬性放到手或遊戲中只有一種策略使用,而不是其他策略。 我的問題是:什麼是最好的面向對象的方式?爲該策略創建特定的類?
class HandForStrategyA extends Hand
{
Int x; // useful only in the strategy A context
}
class StrategyA
{
Game game;
HandForStrategyA hands[];
}
這似乎是一個明顯的答案,但我仍然想知道是否有更好的方法。此外,我有一個語義問題:我應該給我的班級什麼樣的名字(HandFor ...感覺不好!)。 我是一位老派程序員,程序非常流暢,我失去了那麼多時間來思考「乾淨」的面向對象!
重要:每個手的特定屬性。那麼,我必須創建一個繼承Hand類的類,只是爲了我的策略?有沒有其他的方法? – Antoine 2010-12-06 16:20:39