1 / 18

The Singleton Pattern

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

Download Presentation

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. The Singleton Pattern 指導教授: 張顧耀 博士

  2. 第八組 碩士在職專一班 E9306005鄭富聰 E9306003徐明國 E9306012石國堂 E9306004李國陽 E9306011林育俊 E9306007林永鎮 E9306006陳文賢 (先後序)

  3. 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.

  4. 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.

  5. 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.

  6. Introducing the Singleton Pattern • Without requiring the client objects to be concerned • with whether it already exists or not .

  7. Introducing the Singleton Pattern • Singleton Structure Singleton -static instance -singletonData +static getInstance() +SingletonOperation() +getSingletonData() return instance

  8. 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; • }}

  9. 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.

  10. 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.

  11. 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.

  12. Example • Microsoft Visual C++ • main.cpp • singleton.cpp • singleton.h

  13. Non-Software Example • 實體 • 獨一無二 • 容易存取指標 • 總統

  14. 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.

  15. A Variant: The Double-Checked Locking Pattern • Only applies to multithreaded applications

  16. 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; } }

  17. Summary • To ensure that there is only one instance of an object. • Reduce name space. • Permits a variable number of instances.

  18. 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

More Related