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.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Chapter 5: The Singleton Pattern
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
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.
Rounding Out Classes The objectives of this chapter are: To discuss issues surrounding passing parameters to methods What is "this"? To introduce class.
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.
Structured Programming and UML Overview Session 2 LBSC 790 / INFM 718B Building the Human-Computer Interface.
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.
Java Interfaces Overview Java Interfaces: A Definition.
Chapter 9: Classes with Instance Variables or Classes=Methods+Variables Asserting Java © Rick Mercer.
UML Class Diagram: class Rectangle
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
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.
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
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.
Singleton and Basic UML CS340100, NTHU Yoshi. What is UML Unified Modeling Language A standardized general-purpose modeling language in the field of software.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
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.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
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.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
OOP Basics Classes & Methods (c) IDMS/SQL News
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Introduction to Object-oriented Programming
Design Patterns: Brief Examples
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
UML Class Diagram: class Rectangle
Design Patterns in Operating Systems
PH page GoF Singleton p Emanuel Ekstrom.
Interface.
Simple Classes in C# CSCI 293 September 12, 2005.
What is Singleton Category: Creational pattern
METHOD OVERRIDING in JAVA
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
CS 350 – Software Design Singleton – Chapter 21
Outline Anatomy of a Class Encapsulation Anatomy of a Method
CSG2H3 Object Oriented Programming
Presentation transcript:

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 one audio clip to play at a time

2 Goal: You want to control all references to the AudioClip, and reference at one time only one AudioClip – In sum, you’ll want to manage the AudioClip. (I borrowed this example from Patterns in Java)

3 How would you design this? Not implement the functionality, or Create a class with only static methods, or Create a global variable to reference the clip, or Create only one instance of the object that plays the clip; reference only that object to play/stop/etc.

4 Problems with Static Functions/Global Var Data not protected from errant behavior; can be changed by any other part of application Decrease your chances of writing modular code (refactor or enhance) Objects no longer hold state and behavior

5 Singleton Pattern Definition: Job of the Singleton is to enforce the existence of a maximum of one object of the same type at any given time. It does this by providing a global point of access to the object. Creational Patterns Singleton Singleton belongs to the family of Creational design patterns, those patterns that govern the instantiation process.

6 GofF UML Diagram This Singleton class controls access to the object, with the static Instance returning a reference to the unique Instance of the class This is the unique instance of the Class that shouldn’t have multiple instantiations

7 Scenario: MessageLogManager Imagine that you want all messages, warnings, etc., displayed to the user to also be written to a log file. You don’t want to create multiple LogWriters, nor do you want to get into the trouble of trying to write to the log file two different messages at the same time.

8 LogManager UML Diagram This class manages the writing to the log. Note that the constructor is private. The method getInstance is static and public. You call the getInstance() method that returns one MessageLogManager.

9 Implementations C++ is implemented with examples in the Design Patterns Book (Maze Example) Java example is the LogManager singleton diagrammed above.

10 Java Implementation public class MessageLogManager { private static MessageLogManager instance = new MessageLogManager(); //this could be a Message object; for simplicity, it is a string here private String message = "log message"; /*Constructor; must be declared -- if it isn't, a public constructor is automatically created! */ private MessageLogManager() { } public static MessageLogManager getInstance() { return instance; } public void printMessage() { System.out.println(message); }

11 Java Test Java Test import MessageLogManager; class TestSingleton { public static void main(String[] args) { MessageLogManager mlm = MessageLogManager.getInstance(); System.out.println(mlm); mlm.printMessage(); System.out.println(mlm); }

12 Ugly, but validating results log message

13 Possible Pitfalls 1.Depending on your implementation, your Singleton class – and all of its data – might be freed or garbage collected. If you cannot ensure a live reference to your Singleton class, you will need to force one. 2.The beauty of the Singleton pattern is NOT because the class is defined as a static object, but because the method or function is static, and the constructor is private. GofF: “A client that tries to instantiate Singleton directly will get an error at compile-time. This ensures that only one instance can ever get created.” 3.Subclassing: Place the getInstance() method in your subclass, and not in the parent class (parent class could be an interface?). Registry of Singletons (beyond the scope of this presenter). 4.Multiple threads will also need to be carefully examined so a. one instance is created, and b. thread synchronization stays simple.