user810430 user810430 - 28 days ago 15
Java Question

What is the right pattern?

What is the correct way to resolve:

Class A {

methodDoSomeOperation() {

ClassB b.someOperation(.....);
....

}


ClassB {

someOperation(....) {


if (this.someCondition) then{
ClassC c.anotherOperation(....)
} else {
ClassD d....
}
}

}


Methods
c.anotherOperation
takes a long time. I would like receive notification regarding what was selected:
c.anotherOperation(....)
or
d.....
for display in a user interface before invoking
c.anotherOperation
.

Something like:

someOperation(....) {


if (this.someCondition) then{
//notify ClassA what was selected and continue
ClassC c.anotherOperation(....)
} else {
ClassD d....
}
}


Obviously it is possible to invoke some method in
ClassA
but this creates strong coupling and confusing logic. Is it possible use some features of Spring?

Answer

Would you consider some form of the Observer pattern? Whatever the class that is closer to the UI(May be class A?) can be registered as an observer to the Class B. this can be done upfront or as part of the call to the B.SomeOperation() by adding a parameter. Then when you do the check B.someCondition, you can call the registered observer an then call the long running operation on C.

Comments