我給了這組代碼,需要提出一些方法來改進代碼的內聚和耦合類。但是我認爲這些類很好地解耦,因爲它看起來好像在利用事件。就凝聚力而言,所有的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之間應該有什麼關係?你可以改變你的例子有意義的類嗎?這個''不能從靜態的上下文訪問,順便說一句。 – Atreys 2011-06-16 19:12:56