Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 如何理解属性是类的成员还是我必须创建一个不同的类来保存它?_Oop_Design Patterns - Fatal编程技术网

Oop 如何理解属性是类的成员还是我必须创建一个不同的类来保存它?

Oop 如何理解属性是类的成员还是我必须创建一个不同的类来保存它?,oop,design-patterns,Oop,Design Patterns,有时,当您创建一个类时,您可以在其中添加几个属性(新数据成员),您不确定是否要添加这些属性。例如,我有一个赌场老虎机游戏。我有瓷砖,瓷砖在不同的卷轴上旋转。因此,一旦3张牌出现在同一条线上,那么玩家将赢得3美元,4张牌-4美元,5张牌-5美元,对于牌A和牌B玩家将分别赢得5美元,10美元和20美元。例如,每个磁贴应该存储其奖励的数据,还是应该有一个奖励管理器来检查有多少磁贴彼此相邻以向玩家提供奖励 请注意,我不想有。但我发现我多次在想“我是否应该添加这些数据,以及相应的逻辑是否应该添加到我的类?

有时,当您创建一个类时,您可以在其中添加几个属性(新数据成员),您不确定是否要添加这些属性。例如,我有一个赌场老虎机游戏。我有瓷砖,瓷砖在不同的卷轴上旋转。因此,一旦3张牌出现在同一条线上,那么玩家将赢得3美元,4张牌-4美元,5张牌-5美元,对于牌
A
和牌
B
玩家将分别赢得5美元,10美元和20美元。例如,每个磁贴应该存储其奖励的数据,还是应该有一个奖励管理器来检查有多少磁贴彼此相邻以向玩家提供奖励


请注意,我不想有。但我发现我多次在想“我是否应该添加这些数据,以及相应的逻辑是否应该添加到我的类?”。我担心什么时候需要不同的管理器来处理这些事情,但另一方面,我需要创建几个单例类或类似单例的类。

好吧,这听起来很像是应用程序的用例

就我而言(从未去过赌场,因为在我的国家这里是禁止的),大多数老虎机的工作方式都是一样的

因此,您可以将一个实现看作(伪java代码):

类图{
私有字符串表示;
}
班槽{
私人设定数字;
公众人物getRandom(){
//从插槽中检索随机图形
}
}
界面导向策略{
公共积分奖励(SlotMachine机器);
}
类FolderWardStrategy实现了IRewadStrategy{
公共积分奖励(SlotMachine机器){
返回machine.getCurrentLinedSlotsCount();
}
}
类TenFoldRewardStrategy实现方向策略{
公共积分奖励(SlotMachine机器){
返回10*machine.getCurrentLineDSLotScont();
}
}
类槽机{
私人国际时隙帐户;
私人名单时段;
私有列表结果;
私人董事;
公共插槽机(列出插槽,IREWARDS){
这个。插槽=插槽;
这1.rs=rs;
}
公众登记册(){
//对于每个插槽,获取随机数字
}
公共int getTotalSlots(){
返回时隙计数;
}
public int getCurrentLineDSLotScont(){
//对当前滚动结果进行迭代,并获取行槽数
}
public int getraward(){
this.rs.getReward(this);//委托给奖励策略
}
}
//用法
SlotMachine machine=新SlotMachine(…,新的十倍向上策略());
machine.roll();//假设这一行有3个插槽。。。
打印(machine.getReward());//这将产生30

注意:这是一个非常简单的代码,只是给你一个想法,它有几个问题。

你有没有注意到根据你的代码,奖励取决于瓷砖类型或
图形
类型?当然,代码并不完美,但我看不出你所说的这种依赖性。
class Figure {
    private String representation;
}

class Slot {
   private Set<Figure> figures;
   public Figure getRandom() {
       // retrieve random figure from a slot
   }
}

interface IRewardStrategy {
    public int getReward(SlotMachine machine);
}

class OneFoldRewardStrategy implements IRewardStrategy {
    public int getReward(SlotMachine machine) {
        return machine.getCurrentLinedSlotsCount();
    }
}

class TenFoldRewardStrategy implements IRewardStrategy {
    public int getReward(SlotMachine machine) {
        return 10 * machine.getCurrentLinedSlotsCount();
    }
}

class SlotMachine {
    private int slotCount;
    private List<Slot> slots;
    private List<Figure> currentRollResult;
    private IRewardStrategy rs;

    public SlotMachine(List<Slot> slots, IRewardStrategy rs) {
        this.slots = slots;
        this.rs = rs;
    }

    public void roll() {
        // For each slot, get random figure
    }

    public int getTotalSlots() {
        return slotCount;
    }

    public int getCurrentLinedSlotsCount() {
        // Iterates over the current roll result and get the number of lined slots
    }

    public int getReward() {
        this.rs.getReward(this); // delegates to the reward strategy
    }
}

// Usage

SlotMachine machine = new SlotMachine(..., new TenFoldRewardStrategy());
machine.roll(); // suppose this give 3 slots in a row...
print(machine.getReward()); // This will yield 30