【发布时间】:2021-03-27 16:01:48
【问题描述】:
我正在尝试在 Java 中使用复合设计模式。但是,我不确定我是否有效/正确地使用了它。下面的代码是对复合设计模式的有效/正确使用吗?我有一个类 leafGoal,compositeGoal 扩展了该类。但是,他们没有实现接口,这是我在网上看到的一种常见的模式,有复合模式。这有关系吗?那我没有两个类的通用接口?它仍然是有效的复合模式吗?
public class leafGoal {
String goalName;
//private GoalStrategy strategy;
GoalStrategy strategy;
private Dungeon dungeon;
private Inventory inventory;
public class compositeGoal extends leafGoal {
ArrayList<leafGoal> goals = new ArrayList<leafGoal>();
public compositeGoal(String goalName, GoalStrategy strategy, Dungeon dungeon, Inventory inventory) {
super(goalName, strategy, dungeon, inventory);
}
public void addGoal(leafGoal g) {
goals.add(g);
}
【问题讨论】:
-
复合不是叶子,所以不应该扩展它;并且一个 Composite 可以包含其他 Composite,而不仅仅是 Leaf。
标签: java oop design-patterns composite