Singleton design pattern

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
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
Visibility Larman, Chapter 19 (with ideas from George Blank of NJIT) CSE432 Object Oriented Software Engineering.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Chapter 15 Interaction Diagrams. Most Common Sequence Diagram Communication Diagram Sequence Diagrams illustrate interactions between classes of a program.
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
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/
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.
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
Computer Science 313 – Advanced Programming Topics.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Computing IV Singleton Pattern Xinwen Fu.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
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.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Advanced Programming in Java
Advanced Programming in Java
Unit II-Chapter No. : 5- design Patterns
OOP: Encapsulation &Abstraction
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Design Patterns C++ Java C#.
Use interfaces to define constants?
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Design Patterns C++ Java C#.
Creational Design Patterns
Singleton Pattern Command Pattern
Creational Patterns (2)
Can perform actions and provide communication
CS240: Advanced Programming Concepts
PH page GoF Singleton p Emanuel Ekstrom.
Can perform actions and provide communication
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
What is Singleton Category: Creational pattern
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton …there can be only one….
Can perform actions and provide communication
Advanced Programming in Java
CS 350 – Software Design Singleton – Chapter 21
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.
Object Oriented Programming
Presentation transcript:

Singleton design pattern Nima Yahyazadeh

Intent Only one instance Providing global point of access Just-in-time initialization or Initialization on first use

How? A single class responsible for: Private static data member Creation Initialization Access Enforcement Private static data member Provide public static function Client calls accessor function

How? Initialization on first use

Java Code public class Singleton { // Private constructor prevents instantiation from other classes private Singleton() {} /** * SingletonHolder is loaded on the first execution of Singleton.getInstance() * or the first access to SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() return SingletonHolder.INSTANCE;

Office of the U.S president Only one president at a time Global point of access Regardless of president name

Ideas Initialization and global access Can we extend it? Singleton and subclasses Deleting object a singleton pattern

Sources sourcemaking.com/design_patterns/singleton sourcemaking.com/design_patterns/to_kill_a_singleton http://apachetechnology.in/ati/www/KC/dw/Design%20P atterns%20Explained.pdf