Observable pattern implementation in Java -


i using java's observer/observable pattern , wondering: why there need of setchanged() method in current implementation ? understand here have call notifyobservers() once, @ end of our treatment.

that way, if want can rollback changes clearchanged(). still, checking in our own implementation, , call notifyobservers() when absolutely want update observers.

i'm missing something, don't understand why didn't simplify way. ideas?

the intent keep separate concerns apart. notifying observers can called in 1 place, while keeping track of changes happens in place. times don't need kind of separation, java.util.observable implementation designed able handle complex cases, don't have 1-1 correspondence between changes , notifications.

for instance have requirement throttle notifications avoid flooding clients events, shouldn't affect tracking changes occur. example below uses worker thread periodically calls notification method doesn't know changes, , other code accepts changes doesn't handle notification.

import java.util.*;  public class observableexample {      public static void main(string[] args) throws exception {         counttracker t = new counttracker();         t.addobserver(new observer() {             public void update(observable observable, object arg) {                 system.out.println("observed " + arg);             }         });         t.startnotifying();         (int = 0; < 100; i++) {             thread.sleep(100l);             t.incrementcount();         }                  t.quitnotifying();                system.out.println("done");              } }  class counttracker extends observable {         private int count = 0;     private thread notificationthread;      public synchronized void incrementcount() {         count++;         setchanged();     }      public synchronized void startnotifying() {         if (notificationthread == null || !notificationthread.isalive()) {             notificationthread = new thread(new runnable() {                 public void run() {                     try {                         while (!thread.currentthread().isinterrupted()) {                             thread.sleep(1000l);                             string event = "current value of count "                              + counttracker.this.count;                             counttracker.this.notifyobservers(event);                         }                     } catch (interruptedexception e) {                         thread.currentthread().interrupt();                     }                 }             });             notificationthread.start();         }     }      public synchronized void quitnotifying() throws interruptedexception {         if (notificationthread == null || !notificationthread.isalive()) {             return;         }         notificationthread.interrupt();         system.out.println("wait notification thread terminate");         notificationthread.join();     }  } 

a real-life example similar implementing progress bar, requires translating accumulated inputs representing tasks getting done completion percentage , deciding how update ui display.

also consider can subclass java.util.observable. jdk library developers didn't want have support huge number of subspecializations of things favored creating classes broadly useful possible, if want eliminate redundancy in how use can create own variation.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -