Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”

Similar presentations


Presentation on theme: "CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”"— Presentation transcript:

1 CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Michael Gardner March 20, 2006

2 Factory Method Defer instantiation to subclasses
Define interface for creating objects Subclasses decide which class to instantiate

3 Applicability Use Factory Method when:
A class can’t anticipate the class of objects it must create A class wants its subclasses to specify the objects it creates Classes delegate to one of several helper subclasses, and you want to localize knowledge about which is the delegate

4 Participants Product ConcreteProduct Creator
Defines interface of objects to be created ConcreteProduct Implements Product interface Creator Declares factory method Returns object of type Product May define default implementation May call factory method to create Products

5 Structure

6 An Example Application framework
App subclasses “Application,” “Document” Framework creates Document, hands it to Application Doesn’t know what type of Document to create Factory Method moves knowledge about specific Document subclass out of framework

7 Consequences Eliminates need to bind creation code to specific subclasses May need to subclass Creator for each ConcreteProduct Provides hooks for subclasses Connects parallel class hierarchies

8 To Kill a Singleton GoF makes no mention of Singleton destructors
Who deletes a Singleton?

9 Who Kills a Singleton? Could make clients handle deletion
Annoying & error-prone Dangling references Since Singleton carries creational responsibility, have it delete as well Protected destructor (for subclassing)

10 When do Singletons Die? Singletons are long-lived
Usually live until program termination Care about orderly shutown rather than deletion of Singleton instance 2 alternatives for program-scope Singletons: Class-static pointer w/ static guard Destructor implcitly called at program end Function-static instance

11 Static Guard: Sample Code
static Singleton * instance; static SingletonGuard guard; ... Singleton::getInstance() { if (instance == 0) guard.setSingleton(instance = new Singleton()); return instance; } SingletonGuard::setSingleton(Singleton s) { singleton = s; SingletonGuard::~SingletonGuard() { delete singleton;

12 Static Guard: Caveats Dependent Singletons
Destructor ordering for statics undefined in C++ Can’t use multiple guards if Singletons’ destructors depend on each other Use multifunctional guard or atexit()

13 Function Static Singleton
Make Singleton instance function-static instead of class-static: Singleton & Singleton::getInstance() { static Singleton s; return s; } Problems: Hard to extend Not thread-safe

14 Singleton Class Templates
Create Singletons by wrapping existing classes in Singleton class template Problems: Doesn’t prevent creation of more instances Compiler issues with class template static data members Can make existing classes “true” Singletons by subclassing: class User: public Singleton<User> {...};

15 Summary Factory Method To Kill a Singleton
Defer object creation to subclasses via “hook” methods and overriding Creation code need not know about specific subclasses To Kill a Singleton Non-public destructor makes Singleton responsible for deletion Static guard can delete instance implicitly


Download ppt "CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”"

Similar presentations


Ads by Google