model view controller - How to Use Java Observer's update(Observable, Object) Function? -


i have basic mvc pattern created in java uses observable/observer class/interface.

observable    observer      observable/observer model         controller    view  view triggers event controller, when user interacts gui. - e.g presses button, fills in field, etc.  model triggers event view when updates state. - e.g when button pressed , controller requests new results. 

my question observer function

update(observable obs, object arg); 

this 1 function, have many different kinds of updating in view example. how elegantly distinguish between update to, say, search results or displaying of additional information? these 2 different updates use different objects model.

my first idea use object pass string describe update required.

"updateresults" "displayadditionalinformation" "addquestions" 

but seems error-prone , ugly. second instinct create eventobject passed object, have keep asking kind of eventobject i'm using:

if (arg instanceof resulteventobject)      // results model else if (arg instanceof informationeventobject)     // information model else if (arg instanceof questionseventobject)     // questions model 

my third idea update everything, seems pointlessly inefficient.

i not understanding observable/observer interface correctly or i'm not using update() intended it's authors. therefore question, how use updatefunction when have many different types of updates or events process?

you can create own listener interfaces depending on view/model listening to. allows view/model pass information required controller , make easier unit test controller.

for listening model, updating simplest solution , can unless performance proves issue.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -