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

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

T O K ILL A S INGLETON F ACTORY M ETHOD P ATTERN Josh Mason 6/18/09.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Factory Method By Judith Mziray And Jerry Cipolla.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
Creational Patterns: The Abstract Factory CSE 335 Spring 2008 E. Kraemer.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
02 - Creational Design Patterns Moshe Fresko Bar-Ilan University תשס"ח 2008.
Software Components Creational Patterns.
12/6/20041 The Factory Method Pattern Presenters 王世賀 F 陳祐毓 F 張峻銘 F 吳佩達 F 林俊成 F 鄭榮智 F 許書豪 F
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Factory Method Chris Colasuonno Also known as “Virtual Constructor”
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
C# G 1 CSC 298 Object Oriented Programming Part 2.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns I.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
The Factory Pattern Sanjay Yadav (ISE ).
Advanced Object-oriented Design Patterns Creational Design Patterns.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Overview of C++ Polymorphism
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Copyright © Jim Fawcett Spring 2017
Design Patterns: MORE Examples
Abstract classes and interfaces
Advanced Programming in Java
Abstract Factory Pattern
Factory Method Pattern
Low Budget Productions, LLC
Factory Patterns 1.
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Software Design and Architecture
Creational Design Patterns
Object-Oriented Programming
Factory Method Pattern
Structs.
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
This pointer, Dynamic memory allocation, Constructors and Destructor
PH page GoF Singleton p Emanuel Ekstrom.
Abstract classes and interfaces
Virtual Functions Department of CSE, BUET Chapter 10.
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Strategy and Template Method Patterns, Single User Protection
Overview of C++ Polymorphism
Abstract classes and interfaces
Creational Patterns.
CS 325: Software Engineering
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
More C++ Classes Systems Programming.
Presentation transcript:

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

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

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

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

Structure

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

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

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

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)

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

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;

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()

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

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

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