I have two classes (A and B) that depend on each other in the following sense: Each class has a method that performs an action.
The action of each class depends on the action of another class.
So, if the user invokes an action of class A, he should automatically invoke the action of class B.
The same goes for the other. But an infinite loop should be avoided.
I found code that addresses this issue, but it seems to be a bit dumb: an endless loop is prevented by locking .
import java.util.concurrent.locks.*;
import static java.lang.System.*;
import org.junit.*;
public class TEST_DependentActions {
static class A {
private B b = null;
private final ReentrantLock actionOnBLock = new ReentrantLock();
public void setB(B b) {
this.b = b;
}
public void actionOnB() {
if (!actionOnBLock.isLocked()) {
actionOnBLock.lock();
b.actionOnA();
actionOnBLock.unlock();
}
}
}
static class B {
private A a = null;
private final ReentrantLock actionOnALock = new ReentrantLock();
public void setA(A a) {
this.a = a;
}
public void actionOnA() {
if (!actionOnALock.isLocked()) {
actionOnALock.lock();
a.actionOnB();
actionOnALock.unlock();
}
}
}
@Test
public void test1()
throws Exception {
out.println("acting on class A first:");
A a = new A(); B b = new B();
a.setB(b); b.setA(a);
a.actionOnB();
}
@Test
public void test2()
throws Exception {
out.println("acting on class B first:");
A a = new A(); B b = new B();
a.setB(b); b.setA(a);
b.actionOnA();
}
}
The output is as follows:
acting on class A first:
A : calling class B action.
B : calling class A action.
acting on class B first:
B : calling class A action.
A : calling class B action.
Well, this works, but does not seem to be the optimal solution.
?
, ?
EDIT:
.
, , Element.
remove (someElement),
removeMe().
, ,
.