#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Observer design question


    Hi,

    lets say I have a class company which internally stores a list of employees and departments. Now I want to observe these components for changes (i.e. the name of an employee, the manager of a department or the department in a company itself changed)

    Now, is it a better design to implement an Observer-class who gets called by the internal components (Employee might look like this)

    JAVA Code:
    private void notifyNameChange() {
      List ol = ObserverList.getInstance().getObservers();
      Iterator it = ol.iterator();
      while (it.hasNext()) {
        ((ICompanyObserver)it.next()).employeeNameChanged(this);
      }
    }


    or is it better to let Company implement an Observable-Interface in which case every Company component has to notify the company object itself at first.

    Additionally everytime you wish to add a new component to company the interface must be changed which is bad design AFAIK.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    It depends on your requirements but generally, if employee name changes are what you wish to observe, then the employee interface should provide for that. That doesn't mean you can't also have an interface at the Company level that is implemented in terms of the employee interface.
    I no longer wish to be associated with this site.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2005
    Location
    Bay Area, California
    Posts
    841
    Rep Power
    1682
    I would have the company/employee/etc domain objects be value types (hold data) but not have any business logic. When an update occurs, it would be through a repository which could then trigger an event notification on a event-bus. The event handlers would be registered with the bus for the value types its interested in, and be sent the change notification. It could then process the change as it sees fit. This approach decouples the event notification/handler from the types, keeps object roles focused, and allows the asynchronous processing either VM-wise or network-wide. If you make it metadata and IoC driven, then it will be easy to add/remove events and handlers over time.
    Core design principles when developing software systems.
    See my open-source project as an example of professional code.
    ---
    The opinions expressed do not represent those of my employer.

IMN logo majestic logo threadwatch logo seochat tools logo