Chapter 5: The Singleton Pattern

Slides:



Advertisements
Similar presentations
Plab – Tirgul 12 Design Patterns
Advertisements

Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
March R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (in Larman) Prototype Singleton.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
1 Scenario: Audio Clip Imagine that your application played an audio clip Based on user action, a different audio clip may begin playing You want only.
Static members Based on Java tutorial by Oracle: svars.html
Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
1 Design Patterns CS 123/CS Outline zDefinition and Description of a Design Pattern zDiscussion of Selected Patterns zKinds of Patterns Reference:
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
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.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Java Coding 5 – Part 2 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. To object or not…
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Memory Management in Java Mr. Gerb Computer Science 4.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns – Chocolate Factory (from Head First Design Patterns)
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Intro To Classes Review
Singleton Pattern Command Pattern
Design Patterns (GoF) contains the creational patterns:
Dynamically Allocated Memory
PH page GoF Singleton p Emanuel Ekstrom.
Programming Design Patterns
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns
SE-2811 Software Component Design
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Singleton …there can be only one….
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Assessment – Java Basics: Part 1
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Design pattern Lecture 6.
Chapter 8, Design Patterns Singleton
5. Strategy, Singleton Patterns
More concurrency issues
עקרונות תכנות מונחה עצמים תרגול 7 – Design Patterns
Threads and concurrency / Safety
Presentation transcript:

Chapter 5: The Singleton Pattern

The Chocolate Factory Example The chocolate factory has computer controlled chocolate boilers. The job of the boiler is to take chocolate and milk, bring them to a boil, and then pass them on to the next phase of making chocolate bars. One of the main functions of the system is to prevent accidents such as draining 500 litres of unboiled mixture, or filing the boiler when is already full, or boiling an empty boiler.

Creating a Single Instance of a Class In some cases it maybe necessary to create just one instance of a class. This maybe necessary because: More than one instance will result in incorrect program behavior More than one instance will result in the overuse of resources More than one instance will result in inconsistent results There is a need for a global point of access How would you ensure that just one instance of a class is created?

Singleton Pattern Overview In some cases there should be at most one instance of a class. This one instance must be accessible by all “clients”, e.g. a printer spooler. This usually occurs when a global resource has to be shared. The singleton pattern ensures that a class has only one instance and provides only one point of entry.

Implementing the Singleton Pattern Implement a private constructor to prevent other classes from declaring more than one instance. Implement a method to create a single instance. Make this method static. Create a lazy instance of the class in the class. Make the data element static.

Thread Example A A dual processor machine, with two threads calling the getInstance() method for the chocolate boiler Thread 1 Thread 2 public stat ChocolateBoiler getInstance() public stat ChocolateBoiler getInstance() if (uniqueInstance == null) if (uniqueInstance == null) uniqueInstance = new ChocolateBoiler() uniqueInstance = new ChocolateBoiler() return uniqueInstance; return uniqueInstance;

Problems with Multithreading In the case of multithreading with more than one processor the getInstance() method could be called at more or less the same time resulting in to more than one instance being created. Possible solutions: Synchronize the getInstance() method Do nothing if the getInstance() method is not critical to the application. Move to an eagerly created instance rather than a lazily created one.

Synchronizing the getInstance() Method Code public static synchronized Singleton getInstance() {… } Disadvantage – synchronizing can decrease system performance by a factor of 100.

Use an Eagerly Created Instance Rather than a Lazy One Code: //Data elements public static Singleton uniqueInstance = new Singleton() private Singleton() {} public static Singleton getInstance() { return uniqueInstance } Disadvantage – Memory may be allocated and not used.

In Summary The singleton pattern ensures that there is just one instance of a class. The singleton pattern provides a global access point. The pattern is implemented by using a private constructor and a static method combined with a static variable. Possible problems with multithreading. Versions of Java earlier that 1.2 automatically clear singletons that are not being accessed as part of garbage collection.