model view controller - java MVC + vetoable/deferred property -
i have interface foomodel
, class defaultfoomodel
have few simple properties. getting stuck on boolean enable
property because want have vetoable/deferred:
public interface foomodel { public boolean isenabled(); /** returns old value */ public boolean setenable(boolean enable); public void addfoolistener(foomodel.listener listener); public void removefoolistener(foomodel.listener listener); public interface listener { public void onfooenableupdate(boolean newenable, boolean oldenable); } }
what want model m central point of access enable
property. here's sequence of events want occur:
- view v (or user of model m) calls
foomodel.setenable(enable)
on arbitrary thread. foomodel may either:
a. call
onfooenableupdate(newenable, oldenable)
on each of listeners in arbitrary orderb. start potentially long sequence of events whereby entity (e.g. controller c) can decide whether enable, , somehow let foomodel know, whereby
enable
property may updated , proceed usual in (a) above.
the thing is, i'm not sure how go doing this.
approach 1: mechanism shouldn't part of model's interface. instead, defaultfoomodel has implementation accepts foocontroller
:
public interface foocontroller { public void onfooenablerequest(boolean newenable, boolean oldenable); }
when defaultfoomodel.setenable()
called, calls foocontroller.onfooenablerequest()
. controller expected return immediately, can take time digest request, , later call defaultfoomodel.updateenable()
causes real onfooenableupdate()
called.
approach 2: build foomodel interface, i'm not sure what.
approach 3: don't build foomodel, have handle regular property. instead have controller listen foomodel.onfooenableupdate() , override setenable right away old value, call foomodel.setenable() later set new value.
any advice?
make foomodel class instead of interface.
the problem see making foomodel interface complex logic needs coded listener mechanism. it's not idea force implements interface have implement listener logic well.
instead, make foomodel class, implement listener interface yourself, , creatively create public methods can overridden need implement more specific functionality.
notifylisteners(boolean newenable, boolean oldenable)
notifies listeners, default calling following method, overridden nothing, or notify conditionallynotifylistener(listener listener, boolean newenable, boolean oldenable)
notifies specific listener
Comments
Post a Comment