Design Pattern – Behavior – Observer

Definition:

To design a consistent communication model between a set of dependent objects and an object that they are dependent on.

  • Dependent object: Observers
  • The object they dependent on: Subject
  • The relationship is 1 Subject – n Observers.  The state of Observers are synchronized with the state of Subject
  • Mechanism: publisher – subscriber.

When to use:

The Observers are interesting in or dependent on the states of a Subject. Each Observer need to know when the subject’s state changed.

Situation:

  • The Observers list are changed dynamically, so Subject CANNOT maintain a statistic list of Observers.
  • The Observers’s state need to synchronize with the subject’s state
  • Lets think about publisher-subcriber. When a publisher publishes a new, all of the subcribers should be notified.

Example use cases:

  • ReportManager (sub), MonthlyReport(obs), YTDChart(obs): when user choose a department from ReportMangaer, the Monthly Report and YTDCharts need to be calculate and report for that particular DEPARTMENT.
  • Quora question topic, followers are observers as they will receive notification when a new answer is coming.

Class diagram:

To implement publisher – subcriber mechanism, the subject must provide an interface (Observable) for register/unregister, and notify the observers. The Observer also need to implement an interface for receiving the notification.

Take the ReportManager as an Example,  ReportManager is a Subject and implement ObservableInterface (register/unregister, notif), and it has own method to make its string ‘depart’ change. Once changed, loop through observers list and call each observer’s interface method refreshData() with ‘this’, to update the data.

On the other hand, Each observer’s refeshData() will check the passing argument, (which is a Subject Interface)  against its registering Subject to prevent unintended call. Then update itself based on the passing argument.

Review question:

  1. Observers can change its subject state during refesh() process, which ll make the flow inconsistent. What the solution?
    – Pass only read-only Subject interface to the observer, not the ConcreteSubject itself. Cause there s no Mutator methods, the observer cannot change.
    – Other method: check Stackflow answer here

Software design pattern – definition

It is a documented best practice or core of a solution that has been applied successfully in multiple environments to solve a recurring problem.

Diffences btw Design Pattern (DP) and Framework are

  • DP is a recurring solutions to problems, FW is a group of component cooperating with each other to provide a reusable architecture for app with a given domain

Here are some common types of DP:

  • Basic (7):

    The most common and important ones that can be found everywhere in OOP and Programming

    1. Interface:
      Use to design a set of service provider classes that offer same service so that client obj can use different classes of service provider objects in a seamless manner without having to alter the client implementation.
      Example: UserNotification from MySQL or XML service
      Modem and FTP protocol
    2. Abstract parent class
      Use to design a framework for the consistent implementation of the functionality common to a set of related classes
    3. Private method
      Only for internal use, outside object cannot accesc
    4. Accesor method
      Method allow client objects (outside) access states (or properties) of an object. Prevent to access states directly so resulting in more maintainable class structure. Useful for validate data
    5. Constant Data manager
      Useful for design an centralized repository for the constant data in an application
    6. Immutable Object
      The object that has its state unchangeable, so that concurrent access to a data object by several client obj not result in race condition. To achive that, just declare the Properties and Class “final“. If have reference instance variable, return a copy of the obj, but not actual object. (Maybe problems, need decide carefully)
    7. Monitor
      To design an object so that it does not produce unpredictable results when more than one thread tries to access the object at the same time in a multithreaded environment. (use synchronize keywords in JAVA)
  • Creational (5)
    1. Factory method
    2. Singleton
    3. Abstract Factory
    4. Prototype
    5. Builder
  • Collectional (4)
    1. Composite
    2. Iterator
    3. Flyweight
    4. Visitor
  • Structural (11)
    1. Decorator
    2. Adapter
    3. Chain of Responsibility
    4. Facade
    5. Proxy
    6. Bridge
    7. Virtual proxy
    8. Counting proxy
    9. Aggregate Enforcer
    10. Explicit Obj Release
    11. Object Cache
  • Behavioral (11)
    • Command
    • Mediator
    • Memento
    • Observer
    • Interpreter
    • State
    • Strategy
    • Null Obj
    • Template Method
    • Object Auth
    • Common Attribute Registry
  • Concurrency
    • Critical section
    • Consisten lock order
    • guarded suspension
    • Read-write lock