Java 掌握-信息专家模式-方法
在下面的例子中,我正在寻找最佳实践方法。我有三个Java类:Java 掌握-信息专家模式-方法,java,design-patterns,modeling,grasp,Java,Design Patterns,Modeling,Grasp,在下面的例子中,我正在寻找最佳实践方法。我有三个Java类:ManualComponent、AutomaticComponent和CustomComponent,它们扩展了抽象类CalculationComponent,并实现了以下CalculatableComponent接口: public interface CalculableComponent { void calculate(); } 还有另一个类,它在ComponentBox中聚合这些CalculationComponen
ManualComponent
、AutomaticComponent
和CustomComponent
,它们扩展了抽象类CalculationComponent
,并实现了以下CalculatableComponent
接口:
public interface CalculableComponent {
void calculate();
}
还有另一个类,它在ComponentBox
中聚合这些CalculationComponent
:
public class ComponentBox {
private Set<CalculationComponent> components;
public void calculateAll() {
for (CalculationComponent c : components) {
c.calculate();
}
}
}
公共类组件框{
私有集合组件;
公共空间计算{
用于(计算组件c:组件){
c、 计算();
}
}
}
在我被要求更改CustomComponent
中calculate()
方法的实现之前,一切都很顺利。目前,此方法需要来自其他已计算的计算组件的信息(=来自组件框中的设置组件的信息,如计算(组件);
)
我对GRAP的理解是,ComponentBox
类现在是信息专家,因为它包含了进行最终计算所需的所有信息(calculateAll();
)
为了获得最佳实践方法,我应该如何改变我的课程
谢谢你的帮助!
M.信息专家的任务是将一个方法放入正确的类中,即拥有履行职责所需的信息的类。我认为你的计算方法是可以的,但是你遇到的问题是获取信息(因为抽象而无法访问)?你能提供一个具体的例子说明什么不起作用吗?