Observer & singleton pattern

2,293 views

Published on

Published in: Education, Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,293
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
28
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Observer & singleton pattern

  1. 1. 2
  2. 2. 3  Name: Singleton  Intent: 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 one instance 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.
  3. 3. 4
  4. 4. 5
  5. 5. 6  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.
  6. 6. 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
  7. 7. 8  We want to create a remote connection to a server / database system and assure that only one connection is present.  Apply Singleton pattern
  8. 8. 9 Class RemoteConnection{ private static RemoteConnetion remoteConn; private RemoteConnection(){…} //private Constructor public static RemoteConnection getInstance(){ if(remoteConn == null){ remoteConn = new RemoteConnection(); } return remoteConn; } }
  9. 9. In a university….when a student changes his address…pass this information to: Exams department transport department What's the solution? 10
  10. 10. 11 Different Objects Different Interfaces Observers Observable
  11. 11. 12
  12. 12.  Name: Observer  Intent: 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
  13. 13.  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
  14. 14. 15
  15. 15. 16
  16. 16. public interface myObserver{ public void update(Student s); } 17
  17. 17. 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
  18. 18. class TransportDept implements myObserver{ public void update(Student s){ System.out.println("Student Updated in TD"); System.out.println("New Address: "+s.getAddress()); } } 19
  19. 19. 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
  20. 20. 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
  21. 21. 22
  22. 22. 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
  23. 23. 24

×