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.

Slides:



Advertisements
Similar presentations
Chapter 5: The Singleton Pattern
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Plab – Tirgul 12 Design Patterns
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.
Nov R McFadyen1 Design Patterns (GoF) contains the creational patterns: Abstract factory Builder Factory method (section 23.3 has a Simple.
Road Map Introduction to object oriented programming. Classes
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
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.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Factory Design Pattern, cont’d COMP 401 Fall 2014 Lecture 13 10/2/2014.
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.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Problem of the Day  What is the smallest positive integer that cannot be defined in less than twenty-five syllables?
CSC 213 – Large Scale Programming. Why Do We Test?
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.
CSC 212 – Data Structures Lecture 12: Java Review.
CSC 212 Object-Oriented Programming and Java Part 1.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Computer Science 313 – Advanced Programming Topics.
Beware of bugs in the above code; I have only proved it correct, not tried it.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Computer Science 313 – Advanced Programming Topics.
Refactoring1 Improving the structure of existing code.
Computer Science 313 – Advanced Programming Topics.
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.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
CSC 313 – Advanced Programming Topics. Strategy Pattern Usage public class RubberDuck extends Duck { FlightBehavior flyBehavior; QuackBehavior quackBehavior;
2-Dec-15 Inner Classes. 2 Inner classes All the classes so far have been “top level” It is possible (and useful) to define a class inside another class.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Refactoring1 Improving the structure of existing code.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Fred Brooks Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
LECTURE 8: EXCEPTIONS CSC 212 – Data Structures. Error Handling Goals  What should we do when an error occurs?  Should alert system to the error  May.
Eagleson’s Law: Any code of your own that you haven't looked at for 6+ months might as well have been written by someone else.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Inner Classes.
Inner Classes 27-Dec-17.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Improving the structure of existing code
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Lecture 4: Data Abstraction CS201j: Engineering Software
CS 350 – Software Design Singleton – Chapter 21
Designing For Testability
CSG2H3 Object Oriented Programming
Inner Classes 25-Oct-19.
Presentation transcript:

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 telephone.

Computer Science 313 – Advanced Programming Topics

Singleton Pattern  System uses exactly 1 instance of the class  Instantiates only 1 instance of the class  Only lets program use single instance  Does not restrict methods in any way  Can change fields’ values also  Use static method to access instance  Get instance by calling method anywhere in program  Can store a reference to instance in other classes…  … but doing so considered bad style

Singleton Pattern Guides

Identifing a Singleton  College president?  Canisius College president?  CSC313 Professor?  TV Show theme song?

Common Themes in Singleton  Elements always in Singleton class  Static field holds instance  Instance returned by public static method  Singleton class usually includes…  Private constructor limiting instantiation  But class may include  Protected constructor to allow subclassing  Static method also serves as simple factory

How To Create Factory Singleton public class GameFact { private static GameFact me; protected GameFact() { } public static GameFact instance(int userAge){ if (me == null) { if (userAge < 13) me = new AnimatedGameFact(); else me = new RealisticGameFact(); } return me; } }

How Not To Create Singletons public class PrinterSpool { private static PrinterSpool me; private PrinterSpool() { } public static PrinterSpool instance() { if (me == null) { me = new PrinterSpool(); } return me; } }

Synchronization is Hard  Synchronization very expensive  Insures only 1 instance created  Program must remain usable, however  Easy upgrades not helpful if program unused  4 ways to hide costs of synchronization  Each shifts where real costs occur  Minimize costs with a good choice

When Method Rarely Called

When Instantiation is Cheap

For Use on Any JVM

On Modern (1.5+) JVMs

McCloud!  Uniqueness leads to improvements  Provide universal access  Eliminate unneeded checks for errors  Avoid creating large number of small objects  Make space-time tradeoffs with confidence  Can also make correctness easy  Pretty easy to find object triggering error  Ensure order of actions  Prevents problems over which instance in charge

For Next Class  Lab #5 on web/Angel & due week from Tues.  Implements Decorator pattern & a factory  You can choose the factory to implement  Read article linked to from web/Angel page  Describes how inlining works in Java  Why getters & setters are cheap explained  Shows how optimization cascades to generate really, really big improvements