180 likes | 189 Views
This article introduces the Singleton pattern, describes its key features, and provides a case study example. It also discusses a variant called the Double-Checked Locking pattern. Learn how to ensure only one instance of a class is instantiated.
E N D
The Singleton Pattern 指導教授: 張顧耀 博士
第八組 碩士在職專一班 E9306005鄭富聰 E9306003徐明國 E9306012石國堂 E9306004李國陽 E9306011林育俊 E9306007林永鎮 E9306006陳文賢 (先後序)
Overview(1/2) • Introduce the Singleton pattern • Describe the key features of the Single pattern • Introduce a variant to the Singleton called the Double- • Checked Locking pattern. • Describe some of my experiences using the Singleton • pattern in practice.
Overview(2/2) • Ensure that only one object of a particular class is • instantiated. • Singleton pattern is used in single-threaded application. • Double-Checked Locking pattern is used in multithreaded • applications.
Introducing the Singleton Pattern • Intent by GoF • Ensure a class only has one instance, and provide a • global point of access to it. • Define the constructor of this class to be protected • or private.
Introducing the Singleton Pattern • Without requiring the client objects to be concerned • with whether it already exists or not .
Introducing the Singleton Pattern • Singleton Structure Singleton -static instance -singletonData +static getInstance() +SingletonOperation() +getSingletonData() return instance
The Case Study • Example Java Code Fragment:Single Pattern • Class USTax{ • private static USTax instance; • private USTax(); • public static USTax getInstance(){ • if(instance==null) • instance=new USTax(); • return instance; • }}
Key Features(1/3) • Intent • You want to have only one of an object but there is no • global object that controlls the instantiation of this object. • Problem • Serveral different client objects need to refer the same • thing and you want to ensure that you do not have more • than one of them. • Solution • Guarantees one instance.
Key Features(2/3) • Participants and Collaborators • Client create an get instance of the Singleton solely through the instance method. • Consequences • Clients need not concern themselves where an instance of • the Singleton exists. This can be controlled from within the Singleton.
Key Features(3/3) • Implementation • . Add a private static member of the class. • . Add a public static method that instantiates this class. • . Sets the constructor’s status to protected or private.
Example • Microsoft Visual C++ • main.cpp • singleton.cpp • singleton.h
Non-Software Example • 實體 • 獨一無二 • 容易存取指標 • 總統
A problem with Singleton pattern • Suppose two calls to getInstance() are made at exactly • the same time. • It may or may not be a problem.
A Variant: The Double-Checked Locking Pattern • Only applies to multithreaded applications
Example 16-2 Java Code Fragment:Instantiation Only Class USTax extends CalcTax{ private USTax instance; private USTax(){} private synchronized static void dosync(){ if(instance==null) { instance=new USTax(); } } public USTax getInstance(){ if(instance==null){ USTax.doSync(); } return instance; } }
Summary • To ensure that there is only one instance of an object. • Reduce name space. • Permits a variable number of instances.
Reference • Alan Shalloway and James Trott,Design Patterns Explained, Addison-Wesley, 2002. (ISBN: 0-201-71594-5)Web site: http://www.netobjectives.com/dpexplained • E. Gamma, E. Helm, R. Johnson and J. Vlissides,Design Patterns - Elements of Reusable Object-Oriented Software,Addison-Wesley, 1995. (ISBN: 0-201-63361-2)Also known as GoF (Gang of Four)A must for C++ developers.Web site: http://hillside.net/patterns/DPBook/DPBook.html • 『Java設計模式』學習心得 • http://www.dotspace.idv.tw/Patterns/Jdon_DesignPatterns.htm • MSDN