我为扑克手牌历史文件解析器(PHP 或 Java 代码)实现了一个策略模式。我在为几种策略使用的类和对象创建一个干净的 OOP 方式时遇到了问题。
在真实生活中 :
我有一个有手的类游戏
class Hand
{
... // attributes for all strategies
... // (in fact : attributes that define the "core" of a Hand)
}
class Game
{
Hand hands[];
}
一种策略将创建一个对象 Game and the Hands
class StrategyA implements IStrategy // strategy pattern
{
Game game;
function Parse()
{
game = new Game();
...
}
}
而且这种策略需要特定的游戏属性,我不能将这些属性放入手 或游戏中,尽管只有一种策略使用,而其他策略不使用。我的问题是:最好的 OOP 方式是什么?为策略创建特定的类?
class HandForStrategyA extends Hand
{
Int x; // useful only in the strategy A context
}
class StrategyA
{
Game game;
HandForStrategyA hands[];
}
这似乎是显而易见的答案,但我仍然想知道是否有更好的方法。此外,我在语义上有一个问题:我应该给我的班级起什么样的名字(HandFor...感觉不好!)。我是一个老派程序员,非常程序化,我浪费了很多时间来思考“干净的”OOP!