Singleton Pattern Pattern Name: Singleton Pattern Context

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Visibility Larman, Chapter 19 (with ideas from George Blank of NJIT) CSE432 Object Oriented Software Engineering.
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.
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.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
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.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
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.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
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.
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.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
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.
Session 6 Comments on Lab 3 & Implications of Inheritance.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
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.
Generator Design Patterns: Singleton and Prototype
OOP: Encapsulation &Abstraction
Section 9: Design Patterns
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Best Practices and Architecture
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Intro To Classes Review
Design Patterns (GoF) contains the creational patterns:
Chapter 6: Using Design Patterns
Section 9: Design Patterns
CS240: Advanced Programming Concepts
PH page GoF Singleton p Emanuel Ekstrom.
עקרונות תכנות מונחה עצמים תרגול 9 – Design Patterns
SE-2811 Software Component Design
08/15/09 Design Patterns James Brucker.
Object Oriented Design Patterns - Creational Patterns
CSE 1030: Implementing Mixing static and non-static features
What is Singleton Category: Creational pattern
Singleton design pattern
Singleton …there can be only one….
Section 9: Design Patterns
Section 9: Design Patterns
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Section 9: Design Patterns
5. Strategy, Singleton Patterns
Creating and Using Classes
עקרונות תכנות מונחה עצמים תרגול 7 – Design Patterns
Presentation transcript:

Singleton Pattern Pattern Name: Singleton Pattern Context 08/15/09 Singleton Pattern Pattern Name: Singleton Pattern Context We want to ensure there is only one instance of a class. All parts of the application should share this single instance. Motivation (Forces) Several objects need to access the same resource, or we want objects to share a resource that is "expensive". Many parts of the program need to access this shared resource. Solution Prevent direct instantiation by making the constructor private. Provide a static accessor method that always returns the same instance of this class (same object).

Singleton Pattern Singleton Single instance of this class 08/15/09 Singleton Pattern 1 Singleton - instance: Singleton <<constructor>> - Singleton( ) + getInstance( ) : Singleton + other methods for the object's behavior Single instance of this class Static accessor for instance

Singleton Pattern Singleton has 3 elements: Singleton 08/15/09 Singleton Pattern Singleton has 3 elements: Singleton - instance: Singleton <<constructor>> - Singleton( ) + getInstance( ) : Singleton (1) private static attribute that is the only instance of this class (2) constructor is private to prevent other classes from creating objects (3) public static accessor returns the single instance of this class.

Example of Singleton Pattern 08/15/09 Example of Singleton Pattern A Store that has only one instance. public class Store { // (1) the single static instance private static Store theStore = null; private List<Transaction> transactions; // (2) private constructor private Store( ) { transactions = new ArrayList<Transaction>( ); } // (3) static accessor method also creates singleton public static Store getInstance() { if ( theStore == null ) theStore = new Store( ); return theStore; lazy instantiation

08/15/09 Lazy Instantiation Means that you create a resource only when it is needed. This avoids creating something that may never be used. // (3) static accessor method creates the singleton public static Store getInstance() { if ( theStore == null ) theStore = new Store(); return theStore; } The store instance is created the first time that getInstance( ) is called, but not before. If getInstance is never called, no Store is created.

Getting the Singleton object 08/15/09 Getting the Singleton object How do other objects get the Store? // in your application use: Store store = Store.getInstance( );

Lazy Instantiation of Loggers 08/15/09 Lazy Instantiation of Loggers Using Log4J you will see a lot of code like this: // Create the logger for this class private static Logger log = Logger.getLogger(...); What if this class never logs any messages? We wasted time and memory creating the logger. So many apps use lazy instantiation: // Don't create logger yet private static Logger log = null; private static Logger getLogger() { if (log == null) log = Logger.getLogger(...); return log; }

08/15/09 Eager Instantiation Eager instantiation means to create the object as early as possible. Eager instantiation is used in cases such as: you want objects created during start-up, either so the application will "fail early" if object can't be created, or to avoid delay while app is running (e.g. a game needs to create a bunch of sprites while running). public class Store { // eager instantiation: create instance when // the class is loaded into memory. private static final Store theStore = new Store();

Consequences of Using Singleton 08/15/09 Consequences of Using Singleton Benefits control access to a single instance reduce name space pollution - better than using a global variable (in languages with global variables) permits a variable number of instances - you can modify the singleton to produce more than one instance, w/o changing other parts of application Disadvantages Singleton cannot be subclassed, since the constructor is private and static getInstance() is not polymorphic. Related patterns Factory Method