应用设计模式设计以下类时最好的方法是什么?
- 牌组 - addCard、deal、shuffle、getTopCard、removeTopCard、removeAllCards
- 手牌 - addCard、removeCard、getCard、removeAllCards
- DiscardPile - addCard、getTopCard、removeTopCard、removeAllCards
- MeldPile - addCard、removeAllCards
(MeldPile 保存表中的所有融合。)
对我来说,我认为 getTopCard
和 removeTopCard
只是 getCard
和 removeCard
的包装,因为它只是获取卡片的顶部位置,然后将其传递给getCard
或 removeCard
。
我应该使用组合吗?战略模式?或者只是创建另一个名为 CardPile 的类并将其用作上述类的基类?如果您能提供这方面的示例代码,我将不胜感激。
What would be the best approach when designing the following classes applying design patterns?
- Deck - addCard, deal, shuffle, getTopCard, removeTopCard, removeAllCards
- Hand - addCard, removeCard, getCard,removeAllCards
- DiscardPile - addCard, getTopCard, removeTopCard, removeAllCards
- MeldPile - addCard, removeAllCards
(The MeldPile holds all the melds in the table.)
For me, I think the getTopCard
and removeTopCard
are just a wrapper of getCard
and removeCard
, as it just get the top position of a card then pass it to getCard
or removeCard
.
Should I use composition? strategy pattern? or just create a another class called CardPile and use it as the base class of the above class? Really appreciate if you could provide a sample code on this.
发布评论
评论(1)
我认为您可以使用如下所示的单个甲板类来实现您想要的目标,该甲板类本质上是 Stack,我不明白为什么任何特定的牌/堆/手不想要大多数(如果不是全部)相同的方法。
我看到的唯一真正的问题是 deal() 方法,这是否应该是 Deck 的责任?就我个人而言,我不这么认为,这让我认为也许你会有一个 Player 类和一个 Dealer 类,它们扩展 Player 并实现发牌的逻辑
I think you can achieve what you want with a single deck class like below which is essentially a wrapper around Stack, I don't see why any particular deck/pile/hand would not want most if not all of the same methods.
The only real problem i see is with the
deal()
method and whether this should be the responsibility of a Deck? Personally I would not think so, this leads me to think that perhaps you would have a Player class and a Dealer class that extends Player and implements the logic of dealing a deck