observer & singleton pattern
DESCRIPTION
TRANSCRIPT
Observer Pattern&
Singleton Pattern
2
Singleton Pattern(Creational)
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.
4
Singleton: Basic Structure
Singleton
new_instance : Singleton
getInstance() : SingletonSingleton()
5
Singleton sequence diagram
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.
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
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()
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;}
}
In a university….when a student changes his address…pass this information to:Exams departmenttransport department
What's the solution?
10
A case study…
11
TransportDept
Student
changeAdd()
ExamDept
Different Objects
Different Interfaces
Observers
Observable
12
Observer 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)
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
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
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
public interface myObserver{public void update(Student s);
}
17
Implementation: Observer interface
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
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
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
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
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
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
24
The End