what does it mean the class knows too much about the other?
Example (adapted here https://courses.cs.washington.edu/courses/cse331/12au/lectures/08-events.pdf )
class AAA {
public void method1() {
method2();
}
public void method2();
}
}
class BBB {
private AAA ref = new AAA();
public void start() {
while(true) {
if (something) {
ref.method1();
}
}
}
}
class Main {
public static void main(String[] args) {
BBB ref = new BBB();
ref.start();
}
}
The document says that
- Main class dependent on BBB
- BBB depends on AAA
And the questions raised
- Is BBB required to depend on AAA?
- Is BBB reusable in a new context?
Regarding 2. - This? And if not, then why? Why does AAA dependence prevent it from being used in a new context? Does context mean "with another class"?
And he says that although the BBB needs to call method1 (), he should not know what this method does. So you have to loosen the dependency (loose coupling) using the interface / abstract class for AAA instead of AAA itself
, :
class AAA extends AbstractAAA {
}
class BBB {
AbstractAAA ref;
public BBB (AbstractAAA param) {
this.ref = param;
}
public void start() {
while(true) {
if (something) {
ref.method1();
}
}
}
}
class Main {
BBB ref = new BBB(new AAA());
ref.start();
}
, BBB, AAA, BBB AAA.
, " ", , Main BBB AAA, BBB AAA , .
, , - , . , , , / /.
. ( ) , ? , , , , ...?
, SO, ...