240 likes | 454 Views
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
E N D
Chapter 21The 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 • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern
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
Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern
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
C++ Code Fragment The Singleton Pattern
Java Code Fragment The Singleton Pattern
Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern
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
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
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
Key Features ofthe Singleton Pattern • GoF reference The Singleton Pattern
Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern
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
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
C++ Code Fragment The Singleton Pattern
Java Code Fragment The Singleton Pattern
Outline • Overview • Introduction of the Singleton Pattern • Key Features of the Singleton Pattern • A Variant: the Double-Checked Locking Pattern • Summary The Singleton Pattern
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