Observer & singleton pattern

24
Observer Pattern & Singleton Pattern

description

 

Transcript of Observer & singleton pattern

Page 1: Observer  & singleton pattern

Observer Pattern&

Singleton Pattern

Page 2: Observer  & singleton pattern

2

Singleton Pattern(Creational)

Page 3: Observer  & singleton pattern

3

Singleton PatternName: SingletonIntent: Ensure a class has only one instance

and provide a global point of access to it Problem: How can we guarantee that one and

only oneinstance of a class can be created? Solution: Define a class with a private

constructor. The class constructs a single instance of itself Supply a static method that returns a reference to the single instance.

Page 4: Observer  & singleton pattern

4

Singleton: Basic Structure

Singleton

new_instance : Singleton

getInstance() : SingletonSingleton()

Page 5: Observer  & singleton pattern

5

Singleton sequence diagram

Page 6: Observer  & singleton pattern

6

Singleton – Example A status bar ….It could be implemented as a

Singleton object, allowing only one instance and a focal point for updates.

One file system, one window manager, one printer spooler, one Test engine, one Input/Output socket , Windows Registry etc.

Page 7: Observer  & singleton pattern

7

Class Singleton {

private static Singleton uniqueInstance = null;private Singleton( ) { .. } // private constructor

public static Singleton getInstance( ) {if (uniqueInstance == null)

uniqueInstance = new Singleton(); // call constructor

return uniqueInstance;}

}

Singleton: Basic Implementation

Page 8: Observer  & singleton pattern

8

Case Study

We want to create a remote connection to a server / database system and assure that only one connection is present.

Apply Singleton pattern

RemoteConnection

remoteConn : RemoteConnection

getInstance() : RemoteConnectionRemoteConnection()

Page 9: Observer  & singleton pattern

9

Implementation: RemoteConnection

Class RemoteConnection{private static RemoteConnetion remoteConn;private RemoteConnection(){…} //private

Constructor

public static RemoteConnection getInstance(){if(remoteConn == null){

remoteConn = new RemoteConnection(); } return remoteConn;}

}

Page 10: Observer  & singleton pattern

In a university….when a student changes his address…pass this information to:Exams departmenttransport department

What's the solution?

10

A case study…

Page 11: Observer  & singleton pattern

11

TransportDept

Student

changeAdd()

ExamDept

Different Objects

Different Interfaces

Observers

Observable

Page 12: Observer  & singleton pattern

12

Observer Pattern

Page 13: Observer  & singleton pattern

Name: ObserverIntent: Define a one-to-many dependency

between objects so that when one object changes state, all its dependents are notified and updated automatically.

Problem: You need to notify a varying list of objects that an event has occurred.

Solution: delegate the responsibility for monitoring an event to a central object.

13

Observer Pattern (Behavioral)

Page 14: Observer  & singleton pattern

Step 1: Make the Observers behave in the same way

Step 2: Have the observers register themselves

Step 3: Notify the observers when the event occurs

Step 4: Get the information from the observable

14

Applying the Observer

Page 15: Observer  & singleton pattern

ConcreteObserver

Observer

Update(s : Subject)

<<<Interface>>>Subject

ObserversList : Vector

register(Obs : myObserver)unRegister(Obs : myObserver)notify()

Notify lets all observers know that event has occured

15

Basic Structure

Page 16: Observer  & singleton pattern

TransportDept ExamDept

myObserver

update(s : Student)

<<<Interface>>>

Student

address : StringmyObs : Vector

register(Obs : myObserver)unRegister(Obs : myObserver)notifyObs()changeAddress()getAddress()

Composition

16

Applying observer to case study

Page 17: Observer  & singleton pattern

public interface myObserver{public void update(Student s);

}

17

Implementation: Observer interface

Page 18: Observer  & singleton pattern

class ExamDept implements myObserver{public void update(Student s){

System.out.println("Student Updated in Exam Dept");

System.out.println("New Address: "+ s.getAddress());}

}

18

Implementation: ExamDept

Page 19: Observer  & singleton pattern

class TransportDept implements myObserver{public void update(Student s){

System.out.println("Student Updated in TD");

System.out.println("New Address: "+s.getAddress());}

}

19

Implementation: TransportDept

Page 20: Observer  & singleton pattern

class Student{private String address;private Vector myObs;

public Student(){ myObs = new Vector(); address = "Rawalpindi"; }

public void register(myObserver obs){ myObs.addElement(obs); }

public void unRegister(myObserver obs){ myObs.remove(obs); }

public void notifyObs(){Enumeration e = myObs.elements();while(e.hasMoreElements()){((myObserver)e.nextElement()).update(this); } }

public void changeAddress(){ address = "Islamabad"; notifyObs(); }

}

20

Implementation: Student

Page 21: Observer  & singleton pattern

class Main{public static void main(String[]args){

Student s = new Student();TransportDept td = new TransportDept();ExamDept ed = new ExamDept();

System.out.println("Present Address:" + s.getAddress());s.register(td);s.register(ed);s.changeAddress();

System.out.println("******Unregister Exam Dept*******");s.unRegister(ed);s.changeAddress();

}}

21

Implementation: Main

Page 22: Observer  & singleton pattern

TransportDept ExamDept

myObserver

update(s : Student)

<<<Interface>>>

Student

address : StringmyObs : Vector

register(Obs : myObserver)unRegister(Obs : myObserver)notifyObs()changeAddress()getAddress()

Composition

22

Applying observer to case study

Page 23: Observer  & singleton pattern

Suppose you are working on an MDI (Multiple Documents Interface) Form that has several child. You need to notify all the child about the changes that occur in MDI form (e.g title changed).

Apply Observer pattern to solve this problem and draw the corresponding class diagram.

23

Exercise

Page 24: Observer  & singleton pattern

24

The End