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.

Slides:



Advertisements
Similar presentations
Systems Architecture Use Case Diagram, System Overview, Class Diagram Design Patterns (weve used) Refactorings (weve used) Table of Contents.
Advertisements

Chapter 5: The Singleton Pattern
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.
Sequence Diagram Objects are represented horizontally across the top of the diagram Each object has a lifeline some exist before and/or after some are.
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.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Fall 2009ACS-3913 Ron McFadyen1 Design Class Diagrams n Class diagram with – classes – associations – attributes – methods – navigability – interfaces,
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
Fall 2009ACS-3913 R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (Simple Factory) Prototype.
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.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Fall 2009ACS-3913 R McFadyen1 GoF Patterns, chapter 26 GoF book presents 23 patterns: Creational – 5 Structural – 7 Behavioural – 11 Larman discusses 7.
Fall 2009ACS-3913 R. McFadyen1 Polymorphism Indirection Pure Fabrication Protected Variations (Law of Demeter) More GRASP Patterns.
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.
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.
Chapter 26 Applying Gang of Four Design Patterns 1CS6359 Fall 2012 John Cole.
Outline Introduction Problem Statement Object-Oriented Design Aspect-Oriented Design Conclusion Demo.
CSSE 374: Introduction to Gang of Four Design Patterns
Factory, Singleton & Strategy All References and Material From: Applying UML and Patterns, 3 rd ed, chpt 26 & 13 BTS530: Major Project Planning and Design.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Software Engineering Design Patterns. Singleton Single instance of class Constructor is private static final Class instance constructed when application.
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.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Singleton Duchenchuk Volodymyr Oksana Protsyk. 2 /48.
Design Patterns By Mareck Kortylevitch and Piotreck Ratchinsky.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
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.
Chapter 16 UML Class Diagrams 1CS6359 Fall 2012 John Cole.
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.
Bjarne Stroustrup I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my.
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Design Patterns – Chocolate Factory (from Head First Design Patterns)
Design Patterns C++ Java C#.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Abstract Factory Pattern
Design Patterns C++ Java C#.
Singleton Pattern Command Pattern
Design Patterns (GoF) contains the creational patterns:
Design Patterns in Operating Systems
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
SE-2811 Software Component Design
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
GoF Design Patterns (Ch. 26)
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Design pattern Lecture 6.
5. Strategy, Singleton Patterns
GoF Patterns Ch. 26.
Presentation transcript:

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 a class has only one instance. Solution: use a static method of the class to return the singleton Factories and Facades are usually singletons. Textbook problem: how do we control the instantiation of the ServicesFactory? Singleton Pattern

Fall 2009ACS-3913 R McFadyen2 Singleton In Java, you would have code such as: Public static synchronized ServicesFactory getInstance() { If (instance == null) Instance := new ServicesFactory() Return instance } Singleton Pattern The point of the above is that only one instance is ever created. If one already exists, then the create is bypassed and the reference to the existing object is returned.

Fall 2009ACS-3913 R McFadyen3 Singleton In Java, you would have code such as: Public static synchronized ServicesFactory getInstance() { If (instance == null) Instance := new ServicesFactory() Return instance } Singleton Pattern The “synchronized” keyword ensures that only one “thread” runs this code at a time. The text refers to a “critical section”.

Fall 2009ACS-3913 R McFadyen4 Singleton Pattern Suppose an instance of the ServicesFactory is created and, later on, a message, getTaxCalculatorAdapter(), is passed to this instance. :Register :ServicesFactory getInstance() ServicesFactory new() getTaxCalculatorAdapter()

Fall 2009ACS-3913 R McFadyen5 Singleton Pattern :Register :AccountingAdapter getAccountingAdapter() > :ServicesFactory new() post() By applying the > stereotype to this object, they are implying that the getInstance method was used to instantiate it.

Fall 2009ACS-3913 R McFadyen6 Singleton Pattern The text makes reference to Lazy Instantiation – The ServicesFactory object was created when it was needed, and not before :Register :AccountingAdapter getAccountingAdapter() > :ServicesFactory new() post()

Fall 2009ACS-3913 R McFadyen7 Patterns Quote from text “To handle the problem of varying interfaces for external services, let’s use Adapters generated from a Singleton Factory” :Register :AccountingAdapter getAccountingAdapter() > :ServicesFactory new()...