设计模式:哪种 UML 关系最能描述此 class?
Design pattern: Which UML relationship best describes this class?
给出以下 class:
public class CardGame extends Game {
private CardDeck[] cardDecks;
public CardGame(int numCardDecks) {
super();
cardDecks = new CardDeck[numCardDecks];
for (int i=0; i < numCardDecks; i ++) {
cardDecks[i] = new CardDeck();
}
}
}
哪个 UML 关系最能描述此 class? (为什么?)
- 聚合
- 作品
- 概括
- 工厂
注:我觉得这道单选题本身就定义不明确。
聚合、组合和泛化是 UML(class 图)表示法,表示不同类型的关系,即逻辑连接的类型。
在你的例子中 'Game' 是 'CardGame' 的概括; 'CardGame 是 'Game' 的专业化。我想说的是,在你的情况下,'CardDecks' 与你的 'Cardgame' 有组合关系,因为你的卡片是在 'CardGame' class 中创建的,如果你删除'CardGame' 即 "implies a relationship where the child cannot exist independent of the parent" (What is the difference between aggregation, composition and dependency?)。但是,如果您将特定的 'CardDecks' 存储在数据库中,或者如果您试图对可以在另一个游戏中使用纸牌的真实世界进行建模,那么它就是聚合。你的 CardDeck class 是一个 'factory method' 因为它是一个创建对象的 class。
我认为这不应该 class 确定为设计模式,因为要成为设计模式,它必须描述软件设计中常见问题的反复出现的解决方案。
"In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations." (https://sourcemaking.com/design_patterns)
Game
概括了 CardGame
因为 CardGame
是从它派生出来的。
我认为 cardDecks
是 CardDeck
的组合,因为 CardGame
控制着它们的生命周期,并且在这段代码的上下文中,它们只能属于 CardGame
.
给出以下 class:
public class CardGame extends Game {
private CardDeck[] cardDecks;
public CardGame(int numCardDecks) {
super();
cardDecks = new CardDeck[numCardDecks];
for (int i=0; i < numCardDecks; i ++) {
cardDecks[i] = new CardDeck();
}
}
}
哪个 UML 关系最能描述此 class? (为什么?)
- 聚合
- 作品
- 概括
- 工厂
注:我觉得这道单选题本身就定义不明确。
聚合、组合和泛化是 UML(class 图)表示法,表示不同类型的关系,即逻辑连接的类型。
在你的例子中 'Game' 是 'CardGame' 的概括; 'CardGame 是 'Game' 的专业化。我想说的是,在你的情况下,'CardDecks' 与你的 'Cardgame' 有组合关系,因为你的卡片是在 'CardGame' class 中创建的,如果你删除'CardGame' 即 "implies a relationship where the child cannot exist independent of the parent" (What is the difference between aggregation, composition and dependency?)。但是,如果您将特定的 'CardDecks' 存储在数据库中,或者如果您试图对可以在另一个游戏中使用纸牌的真实世界进行建模,那么它就是聚合。你的 CardDeck class 是一个 'factory method' 因为它是一个创建对象的 class。
我认为这不应该 class 确定为设计模式,因为要成为设计模式,它必须描述软件设计中常见问题的反复出现的解决方案。
"In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations." (https://sourcemaking.com/design_patterns)
Game
概括了 CardGame
因为 CardGame
是从它派生出来的。
我认为 cardDecks
是 CardDeck
的组合,因为 CardGame
控制着它们的生命周期,并且在这段代码的上下文中,它们只能属于 CardGame
.