Chapter 4 Creational Patterns: Singleton - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Chapter 4 Creational Patterns: Singleton

Description:

Fa ade. Flyweight. Proxy. Abstract factory. Builder. Prototype. Singleton. Object. Interpreter ... Make system independent of how objects are created, composed, ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 13
Provided by: brahimm
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Creational Patterns: Singleton


1
Chapter 4 - Creational Patterns Singleton
CIS 476/566 Software Architecture and Design
Patterns
  • Dr. Brahim Medjahed
  • brahim_at_umich.edu

2
Design Pattern Classification
3
What are Creational Patterns?
  • Abstract the instantiation process
  • Make system independent of how objects are
    created, composed, and represented
  • Give information in
  • What gets created?
  • Who created it?
  • How it gets created?
  • When?

4
Object and Class Creational Patterns
  • Two categories
  • Object creational patterns
  • Focus on the delegation of the instantiation to
    another object (e.g., Singleton, Abstract
    Factory)
  • Class creational patterns
  • Focus on the use of inheritance to decide the
    object to be instantiated (e.g., Factory Method)

5
Relationship Between Creational Patterns
  • Competitors
  • Abstract Factory vs. Prototype
  • Complementary
  • Prototype uses Singleton

6
Singleton Pattern
  • Intent
  • Ensure a class only has one instance, and provide
    a global point of access to it
  • Motivation
  • Many printers but only one spooler
  • Global variable makes an object accessible but
    does not prevent many instances
  • Make the class itself responsible for keeping
    track of its sole instance

7
Using Constructors
  • How do you create an object?
  • Obj new MyObject()
  • If you repeatedly call this how many instance
    of this object created?
  • Public constructor As many calls to new

8
How the Singleton Pattern Works?
  • Idea Have a special method for instantiating the
    desired object
  • When the method is called
  • Check whether the object has already been
    instantiated
  • If it has, the method returns a reference to the
    object
  • Use protected or private constructor to insure
    this is the only way to instantiate an object

9
Using a Private Constructor
  • Public MyClass
  • private MyClass()
  • What does private constructor mean?
  • It can be only instantiated within itself.
  • Well how do you get access to instance of the
    class?
  • Not sure!!! Lets see

10
Using Private Constructor? (contd)
  • Public MyClass
  • private MyClass()
  • public static MyClass getInstance()
  • return new MyClass()
  • Can we do obj MyClassgetInstance()

11
Uniqueness of an Instance
  • Now can we make it such that only one instance of
    MyClass is instantiated?
  • Lets call this Singleton now.
  • Public class MyClass
  • private static MyClass theInstance
  • private MyClass()
  • public static MyClass getInstance()
  • if (theInstance null)
  • theInstance new MyClass()
  • return theInstance

Uniqueness of an Instance
12
An Example
  • See handout given in the class
Write a Comment
User Comments (0)
About PowerShow.com