Singleton Pattern Presented By:- Navaneet Kumar ise2007019.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Chapter 5: The Singleton Pattern
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
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.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Design Patterns William A. Hoffman NYU OOP Class.
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.
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.
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.
2013: J Paul GibsonTSP: Software EngineeringCSC7322/DesignPatterns.1 CSC 7322 : Object Oriented Development J Paul Gibson, A207 /~gibson/Teaching/CSC7322/
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns III.
Software Components Creational Patterns.
In the name of Allah The Proxy Pattern Elham moazzen.
Design Patterns – II Lecture IV. Singleton Pattern Intent – Ensure a class only has one instance, and provide a global point of access to it Motivation.
Computing IV Singleton Pattern Xinwen Fu.
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.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
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.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Design Patterns Definition:
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Creational Design Patterns Yaodong Bi December 21, 2015December 21, 2015December 21, 2015.
Design Patterns Introduction
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Mixing Static and Non-static Singleton 1. Singleton Pattern 2  “There can be only one.”  Connor MacLeod, Highlander.
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
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.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Generator Design Patterns: Singleton and Prototype
Design Patterns: Brief Examples
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Factory Patterns 1.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Software Design and Architecture
Creational Design Patterns
Singleton Pattern Command Pattern
Software Engineering Lecture 7 - Design Patterns
SE-2811 Software Component Design
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Singleton …there can be only one….
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Design pattern Lecture 6.
Real World Scenario Sometimes it's appropriate to have exactly one instance of a class: window manager print spooler Filesystems press Ctrl-F to display.
Creational Patterns.
5. Strategy, Singleton Patterns
Presentation transcript:

Singleton Pattern Presented By:- Navaneet Kumar ise

Singleton Pattern singleton: an object that is the only object of its type ensures that a class has at most one instance provides a global access point to that instance It is a creational pattern.

Intent It’s important for some classes to have exactly one instance. How we ensure that a class has only one instance and the instance is easily accessible ??? A global variable makes an object accessible but it doesn’t keep you from instantiating multiple objects. Motivation Ensure a class only has one instance, and provide a global point of access to it.

A better solution is to make the class itself responsible for keeping track of its sole instance. The class can ensure that no other instance can be created,and it can provide a way to access the instance.

Applicability Use singleton pattern when: There must be exactly one instance of a class, and it must be accessible to client from well-known access point.

Structure Class Diagram:

Participants Singleton Defines an instance operation that lets clients access its unique instance. May be responsible for creating its own unique instance.

Collaborations Client access a Singleton instance solely through Singleton’s instance operation. Sequence diagram:

Consequences Controlled access to sole instance: Because the Singleton class encapsulates its sole instance, it can have strict control over how and when client access it. Define one value shared by all instances by making it static.

Implementation OK, so how do we implement the Singleton pattern? We'll use a static method to allow clients to get a reference to the single instance and we’ll use a private constructor! /** * Class Singleton is an implementation of a class that * only allows one instantiation. */ public class Singleton { // The private reference to the one and only instance. private static Singleton uniqueInstance = null; // An instance attribute. private int data = 0;

/** * Returns a reference to the single instance. * Creates the instance if it does not yet exist. * (This is called lazy instantiation.) */ public static Singleton instance() { if(uniqueInstance == null) uniqueInstance = new Singleton(); return uniqueInstance; } /** * The Singleton Constructor. * Note that it is private! * No client can instantiate a Singleton object! */ private Singleton() { } // Accessors and mutators here! }

Here's a test program: public class TestSingleton { public static void main(String args[]) { // Get a reference to the single instance of Singleton. Singleton s = Singleton.instance(); // Set the data value. s.setData(34); System.out.println("First reference: " + s); System.out.println("Singleton data value is: " +s.getData());

// Get another reference to the Singleton. // Is it the same object? s = null; s = Singleton.instance(); System.out.println("\nSecond reference: " + s); System.out.println("Singleton data value is: " + s.getData()); } output: First reference: Singleton data value is: 34 Second reference: Singleton data value is: 34

Note that the singleton instance is only created when needed. This is called lazy instantiation. Thought experiment: What if two threads concurrently invoke the instance() method? Any problems? Yup! Two instances of the singleton class could be created! How could we prevent this? Several methods: Make the instance() synchronized. Synchronization is expensive, however and is really only needed the first time the unique instance is created. Do an eager instantiation of the instance rather than a lazy instantiation

Here is Singleton with eager instantiation. We'll create the singleton instance in a static initializer. This is guaranteed to be thread safe. public class Singleton { private static Singleton uniqueInstance = new Singleton(); private int data = 0; public static Singleton instance() { return uniqueInstance; } private Singleton() {} // Accessors and mutators here! }

Known Uses One file system one window manager one printer spooler one Test engine one Input/Output socket and etc

Related patterns Abstract factory, which is often used to return unique objects. Abstract factory Builder, which is used to construct a complex object, whereas a singleton is used to create a globally accessible object. Builder Prototype, which is used to copy an object, or create an object from its prototype, whereas a singleton is used to ensure that only one prototype is guaranteed. Prototype

References: Head First design Patterns >Freeman & Freeman