oop - Notifying observers from nested structures -



oop - Notifying observers from nested structures -

currently, trying dissect tight coupling between info model , ui in application introducing mvc pattern. basically, means making model aware of associated views in order have them informed whenever properties within model change.

the info model represented nested structure:

model - node - leaf - leaf - node - leaf

each element inherits mutual abstract base of operations class (structurednode).

the problem i've been thinking is: observers should able subscribe model (and model), each element should able emit alter notifications. achieved in 2 ways - either notifications routed hierarchy until reach model, pushed observers, or implementing notifications in base of operations class, static observers list, in example:

class="lang-java prettyprint-override">public abstract class base of operations { private static map<iobserver> observers; protected synchronized void internalsubscribe(final iobserver observer) { observers.add(observer); } protected synchronized void notifyobservers(final base of operations obj) { (iobserver observer : observers) observer.changed(obj); } // .. other base of operations class operations }

in implementation, model offer public subscribe method, internally delegate protected internalsubscribe method of base of operations class. @ same time, each derivative of base of operations class send alter notification, this:

class="lang-java prettyprint-override">// perform operations alter object's internal state // ... // notify observers notifyobservers(this);

is rather or rather bad practice (using static observers list)? opinions on this? there alternative solutions?

a static observer list not mutual solution, , not consider practice generic utilize model.

observers notified of changes models not interested in. means burden of identifying model firing notification falls on observers, have no , efficient way of doing other going hierarchy , finding root node... lot of code, must potentially implemented several times in several observers. in end simpler observer ignore considerations , update or recalc, when updated model not 1 it's interested in.

of course of study may not apply in case. if know not have more 1 model of class in application, can go solution.

anyway, requirement of having model public subscribe method should not constrain using static observer container share among model instances. said, there way. routing notifications hierarchy imho right way go.

if want avoid routing, take have nodes maintain reference model. may more efficient if know tree construction deep not dynamic. if nodes can moved 1 model another, bit risky , requires farther code maintain reference updated.

oop design-patterns observer-pattern

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -