【发布时间】:2011-06-16 18:17:56
【问题描述】:
我得到了这组代码,需要提出改进代码内聚和类耦合的方法。但我认为这些类很好地解耦,因为看起来它们正在使用事件。在凝聚力方面,所有的 init() 调用都放在一起,我觉得一切都很好。
public class A
{
private C t;
private B g;
public static void main(String args[]) {
// Creates t and g.
t = new C();
t.init();
g = new B();
g.init();
g.start(this);
}
pubic void update (Event e)
{
// Performs updating of t based on event
}
}
public class C
{
public C() { ... }
public void init() { ... }
}
public class B
{
public B() { ... }
public void init() { ... }
public void start(A s) {
e = getNextEvent();
while (e. type != Quit)
if (e.type == updateB)
update(e) ;
else
s.update(e) ;
e = getNextEvent();
}
public void update(Event e) { ... }
}
}
还有什么方法可以提高类的凝聚力和耦合度吗?对我来说看起来不错,但我认为我遗漏了一些东西。
感谢您对此提出任何建议。
【问题讨论】:
-
A、B 和 C 之间应该有什么关系?你能改变你的例子来有有意义的课程吗?
this不能从静态上下文访问,顺便说一句。
标签: java oop loose-coupling cohesion