Singleton …there can be only one….

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
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.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
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.
March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.
Static members Based on Java tutorial by Oracle: svars.html
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.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
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.
!!! Global Variables!!! are EVIL SSimply because you just write a school boy/gal?
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Software Design Patterns (2) four commonly used design patterns using php (singleton, factory, adapter & decorator)
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
In the name of Allah The Proxy Pattern Elham moazzen.
Software Engineering Design Patterns. Singleton Single instance of class Constructor is private static final Class instance constructed when application.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Taking Turns. I love playing with toys! Sometimes when I play with toys I want to grab a toy that someone else has. This is not the best choice and can.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
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.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
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.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Patterns in programming
Generator Design Patterns: Singleton and Prototype
CIS 200 Test 01 Review.
MPCS – Advanced java Programming
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Inheritance and Polymorphism
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Singleton Pattern Command Pattern
Week 4 Object-Oriented Programming (1): Inheritance
PRG 421 Education for Service-- snaptutorial.com.
PRG 421 Education for Service-- snaptutorial.com.
PRG 421Competitive Success/tutorialrank.com
CS240: Advanced Programming Concepts
Programming Design Patterns
SE-2811 Software Component Design
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Generics.
CS 350 – Software Design Singleton – Chapter 21
Software Design Patterns (2)
SE-2811 Software Component Design
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
CS 325: Software Engineering
5. Strategy, Singleton Patterns
More concurrency issues
GoF Patterns Ch. 26.
Presentation transcript:

Singleton …there can be only one…

The Singleton pattern In some cases, we only want one instance of a class to be created…ever! Could be a class which manages access to some critical resource A database manager A device driver A manager of application preferences … RHS – SWC

The Singleton pattern How can we implement such a restriction? Should we throw an exception, if someone tries to create a second instance…? DBManager dbm1 = new DBManager(); // OK ... DBManager dbm2 = new DBManager(); // Error! Not really what we want… RHS – SWC

The Singleton pattern Clients Creator DBManager Please give me a reference to the (only) DBManager object OK OK (hey, I already have it! DBManager Please give me a reference to the (only) DBManager object RHS – SWC

The Singleton pattern A client should not be able to create an object directly (new …) – he must ask a creator for it Creator creates an object (unless it has already been created), and hands a reference back to the client How do we prevent clients from creating objects themselves…? RHS – SWC

The Singleton pattern public class DBManager { public DBManager() {} … RHS – SWC

The Singleton pattern public class DBManager { private DBManager() {} … } RHS – SWC

The Singleton pattern Private methods can only be invoked by other methods in the same class The class itself becomes respon-sible for creating objects of itself… RHS – SWC

The Singleton pattern public class DBManager { private DBManager() {} public DBManager createInstance() return new DBManager(); } RHS – SWC

The Singleton pattern public class DBManager { private static DBManager theInstance = null; private DBManager() {} public static DBManager getInstance() if (theInstance == null) theInstance = new DBManager(); return theInstance; } RHS – SWC

The Singleton pattern Turning a class T into a Singleton: Make the constructor for T private Add a static variable to T, storing a reference to an object of class T Add a static method to T, which returns a reference to the object pointed to by the static variable. If the reference is null, the method should create an object of type T first, and set the static variable to refer to this object RHS – SWC

The Singleton pattern public class T // T is a Singleton { private static T theInstance = null; private T() {} public static T getInstance() if (theInstance == null) theInstance = new T(); return theInstance; } RHS – SWC

Singleton vs global variables But wait – haven’t we just made a nice wrapping for a global variable? Why not just do like this: public class T { public static T theInstance = new T(); private T() {} ... } RHS – SWC

Singleton vs global variables Issue Singleton Global variable Creation time On demand Initial Access control Possible None Pseudo-uniqueness Destruction order Unpredictable RHS – SWC

Singleton vs global variables A global variable is created when the program starts (eager initialisation) A Singleton is created when somebody asks for it (lazy initialisation) If we only need the object sometimes, it is wasteful to create it unconditionally – in particular if it is resource-intensive RHS – SWC

Singleton destruction We can control when a Singleton is created, but not when it is destroyed Garbage collector should claim it when noone refers to it anymore Order of destruction might matter! RHS – SWC

Singleton destruction Suppose two singletons DataManager and DBManager are dependent: DataManager DBManager Creation Load app data, using DBManager Connect to DB Destruction Save app data, using DBManager Disconnect from DB RHS – SWC

Singleton destruction If the DBManager is destroyed before the DataManager, there might be a problem! Somewhat difficult to manage the dependen-cies between singletons, but it can be done… RHS – SWC

Singleton– final remarks Singleton is a useful – but also debated design pattern (see the Net) Using Singletons in multi-threaded scenarios requires some enhancements You cannot make a ”generic” Singleton class, and let your class inherit from it – who ”owns” the static object reference…? RHS – SWC