1 / 20

Chapter 21 The Singleton Pattern

Chapter 21 The Singleton Pattern. Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Outline. Overview Introduction of the Singleton Pattern Key Features of the Singleton Pattern

nell
Download Presentation

Chapter 21 The Singleton Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 21The Singleton Pattern Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

  2. Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern

  3. Overview • The Singleton pattern and the Double-Checked pattern • Very simple and very common • Ensure that only one object of a particular class is instantiated • Distinction • The Singleton pattern • Used in single-threaded applications • The Double-Checked Locking pattern • Used in multithreaded applications The Singleton Pattern

  4. Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern

  5. Introduction ofthe Singleton Pattern • Intent • Ensure a class only has one instance, and provide a global point of access to it • How it works? • A special method used to instantiate the desired object • Check to see if the object has been instantiated • If it has, the method returns a reference of the object • If not, the method instantiates it and returns a reference of the object • The constructor is defined to be protected or private The Singleton Pattern

  6. C++ Code Fragment The Singleton Pattern

  7. Java Code Fragment The Singleton Pattern

  8. Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern

  9. Key Features ofthe Singleton Pattern • Intent • You want to have only one of an object but there is no global object that controls the instantiation of this object • Problem • Several different client objects need to refer to the same thing and you want to ensure that you do not have more than one of them The Singleton Pattern

  10. Key Features ofthe Singleton Pattern • Solution • Guarantees one instance • Participants and Collaborators • Clients create an get Instance of the Singleton solely through the instance method • Consequences • Clients need not concern themselves whether an instance of the Singleton exists. This can be controlled from with the singleton. The Singleton Pattern

  11. Key Features ofthe Singleton Pattern • Implementation • Add a private static member of the class that refers to the desired object (initially, it is NULL). • Add a public static method that instantiates this class if this member is NULL (and sets this member’s value) and then returns the value of this member. • Set the constructor’s status to protected or private so that no one can directly instantiate this class and bypass the static constructor mechanism. The Singleton Pattern

  12. Key Features ofthe Singleton Pattern • GoF reference The Singleton Pattern

  13. Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern

  14. Double-Checked Locking Pattern • This pattern only applies to single-threaded applications • A problem may arise in multithreaded applications • Two calls to getInstance() are made at exactly the same time • Having some state vs. stateless • An extra bit of memory vs. memory leak The Singleton Pattern

  15. Double-Checked Locking Pattern • A simple solution: double-checked locking • Do a “synch” after the test for NULL and then check again to make sure the instance member has not yet been created. The Singleton Pattern

  16. C++ Code Fragment The Singleton Pattern

  17. Java Code Fragment The Singleton Pattern

  18. Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern

  19. Summary • The Singleton and Double-Checked Locking patterns are common patterns • Ensure there is only one instance of an object • The Singleton is used in single-threaded applications • The Double-Checked Locking pattern is used in multithreaded applications. The Singleton Pattern

  20. The End

More Related