observerpattern

Upload: paramjeet-singh

Post on 08-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 ObserverPattern

    1/3

    ASSIGNMENT 3

    1) Observer Pattern:

    1.1 Goal: Observer Pattern provides an object design such that if state of one of the objectchanges then all of its dependents are notified.

    1.2 Basic Concept Of Observer Pattern : Observer Pattern has concept of two types of classes called subject classes and set

    of Observer classes. The only thing subject knows about observer is that it

    implements interface called Observer interface.

    The Subject class Object contains the state and when its state changes, theobserver design allows notification to be send to all the Observer Concrete

    classes.

    In Observer Pattern, we have one to- many relationships between a subject objectand set of dependent objects.

    Java has inbuilt Observer interface and the Observable class in the java.util.Observerand java.util.Observable package. The Observable class has methods

    o addObserver( ) ----- ----- To add the new Observer.o deleteObserver( ) -------- To delete the Observero notifyObserver( )--------- To notify Observer that Subject state has been changed.o setChanged()----------To signify that the state has been changed in the subject

    class

    Observer interface has update () function which needs to be defined in all theObserver classes implementing the observer interface.

    The figure below shows the high level parallel structure of the Observable Classeswith one Subject class extending it and other side consists of Observer Interface

    and many Observable Classes implementing the interface.

  • 8/7/2019 ObserverPattern

    2/3

    1.3 Implementation in Assignment: The class structure consists of two parallel hierarchies one consist of Subject

    Classes (Observable) and other consist of Observer Classes.

    The Observable Class (in built class of Java in package java.util.Observable) isbeing extended by Subject Class.

    The Observer Interface is being implemented by Observer Classes arepublicMethod, StaticMethod, ProtectedMethod, PrivateMethod,PackageMethod, NonStaticMethod, AllMethods. All these Observer classes have

    defined the interface Update ( ) function which counts the number of Public,

    Static, Protected, Private, Package, NonStatic, All Methods respectively called in

    the Main Program.

    The Subject Class consists of the definitions of the Java Public, Private, Protected,Package, Static and Non Static functions.

    The logic implemented is such that whenever we call any function from Main thenusing Reflection Java Concept we calculate the access modifier of the called

    function at run time and then call setcounter() function defined in subject class to

    change the state of the object by passing calculated access modifier as parameter

    to it. The setcounter() function will call setChanged() and notifyObservers()

    method of the Observable class for notification.

  • 8/7/2019 ObserverPattern

    3/3

    The notifyObservers() method will automatically call or notify update function ofall the registered Observers. The Observers are being registered by using the

    addObserver() method of the Observable class.

    The figure below shows the Class structure implemented for the assignment tohave more clarity on this.

    Summary: The implementation is done in such a way that whenever function is called from

    main program then the state of the subject objectis changed according to the access modifier of

    the called function. Once the state of the object is changed then all the observable classes are

    notified the change using notifyObserver() and setchanged() function.