Design solution for a complication in observer design in java

39 views Asked by At

I have an entity-model defined in POJOs as

class A {
    B instance_b; 
}

class B {
    Collection<C> instance_c;
}

class C {}

I have implemented an observer pattern where there are 2 concrete observers - JsonListener and DatabaseListener, which are used to serialize the singleton instance of class A into json-file (using jackson) and into db as json clob. I have a LifeCycleManager interface with the usual register(), deregister() and notify() methods to add the listeners and notify them

I have written a builder for each of the classes to modify the private attributes (have purposefully not defined any setters). The builder has an update() method via which I want to implicitly notify all the registered listeners.

There are 2 queries -

  1. Should I be implementing the LifeCycleManager interface for all classes A, B, C so that I could invoke the notify() method ? or is there an alternative where this could be avoided since it looks like cumbersome with the entity-model growing.

  2. Since I am writing to a json file, as far as I understand, using any json-library like fasterxml.jackson, I need to RE-WRITE the entire json object (starting from Class A) to the json file. If my understanding is correct, then if I am inside the builder of Class C and I issue the notify() method, how do I notify the listeners with the instance of Class A ? (basically how do I fetch the instance of A from inside class C ?), because just updating Class C instance is not good enough - I need to honour the association. Is there some design I can leverage for the same ?

Please advice or suggest, even if it requires a complete course correction of my line of thought.

Thanks.

0

There are 0 answers