Stéphane Ducasse 1 Singleton.

Slides:



Advertisements
Similar presentations
From Objects to Actors Study of a Limited Symbiosis in Smalltalk-80.
Advertisements

Stéphane Ducasse 1 Design Points - Subclassing vs Subtyping Stéphane Ducasse
S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes... One model applied twice.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Smalltalk Coding Patterns mostly from Smalltalk Best Practice Patterns Kent Beck Prentice-Hall, 1997.
Stéphane Ducasse«ChapterNr».1 Selected Design Patterns Design Patterns are recurrent solutions to design problems They are pros and cons We already saw:
Stéphane Ducasse«ChapterNr».1 Arthur Riel Design Heuristics from Object-Oriented Design Heuristics by Arthur Riel Read the Heuristics… –Find reasons why.
7. Understanding Classes and Metaclasses. © Oscar Nierstrasz ST — Understanding Classes and Metaclasses 7.2 Roadmap  Metaclasses in 7 points  Indexed.
Stéphane Ducasse9.1 Inheritance Semantics and Lookup.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Stéphane Ducasse 1 The Taste of Smalltalk.
13. A bit of Smalltalk. © Oscar Nierstrasz 2 Roadmap  The origins of Smalltalk  What is Smalltalk?  Syntax in a nutshell  Seaside — web development.
PZ06BX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ06BX - Introduction to Smalltalk Programming Language.
Some Basic Points on Classes
9. Understanding Classes and Metaclasses. Birds-eye view © Oscar Nierstrasz ST — Introduction 1.2 Reify your metamodel — A fully reflective system models.
Stéphane Ducasse 1 Objects to the Roots: Learning from beauty.
Stéphane Ducasse«ChapterNr».1 Advanced Classes Indexed Classes Classes as Objects Class Instance Variables and Methods Class Variables Pool Dictionaries.
Stéphane Ducasse5.1 Smalltalk in a Nutshell OO Model in a Nutshell Syntax in a Nutshell.
Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse 1 The Taste of Smalltalk.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
S.Ducasse Stéphane Ducasse 1 Selected 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.
S.Ducasse Stéphane Ducasse 1 Smalltalk Coding Idioms.
S.Ducasse Stéphane Ducasse 1 Processes and Concurrency in VW.
S.Ducasse Stéphane Ducasse 1 Classes and Metaclasses - an Analysis.
S.Ducasse Stéphane Ducasse 1 Design Heuristics.
Stéphane Ducasse 1 Elements of Design - Simple Smells.
Stéphane Ducasse 1 Visitor.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Inheritance Semantics and Method Lookup.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Stéphane Ducasse 1 Elements of Design - Inheritance/Compositio n.
+ Ruby and other programming Languages Ronald L. Ramos.
S.Ducasse 1 7. Understanding Metaclasses. S.Ducasse 2 Roadmap Metaclasses in 7 points Indexed Classes Class Instance Variables Class Variables Pool Dictionaries.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
S.Ducasse Stéphane Ducasse 1 Essential OO Concepts Stéphane Ducasse.
S.Ducasse Stéphane Ducasse 1 Common Mistakes and Debugging in VW.
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
Stéphane Ducasse 1 Some Advanced Points on Classes.
Lecture 4 Page 1 CS 111 Online Modularity and Virtualization CS 111 On-Line MS Program Operating Systems Peter Reiher.
Stéphane Ducasse 1 Strategy.
Stéphane Ducasse7.1 Let’s Play Objects Simulate a LAN physically Set up a context for –future chapters –Exercises Some forward references to intriguate.
Inheritance and Polymorphism. Superclass and Subclass Inheritance defines a relationship between objects that share characteristics. It is a mechanism.
Stéphane Ducasse 1 Some Points on Classes.
S.Ducasse Stéphane Ducasse 1 Smalltalk in a Nutshell.
S.Ducasse Stéphane Ducasse savoie.fr e/ e/ 1 Smalltalk in a Nutshell.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Smalltalk Lecture 14 – Smalltalk, Fall CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Stéphane Ducasse 1 Elements of Design - Unit of Reuse.
Module 9. Dealing with Generalization Course: Refactoring.
Stéphane Ducasse 1 Abstract Classes.
S.Ducasse Stéphane Ducasse 1 Some Advanced Points on Classes.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Administrative Issues Lecture 22 – Prolog (III), Fall 2007 CSE3302 Programming Languages, UT-Arlington ©Chengkai Li, HW3 (due at March 18 th ) Essay.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.
S.Ducasse 1 Instance Initialization How to ensure that an instance is well initialized? Automatic initialize Lazy initialization Proposing the right interface.
Design Points - Law of Demeter
Handling Exceptionally Sticky Problems
Section 11.1 Class Variables and Methods
PH page GoF Singleton p Emanuel Ekstrom.
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
CS 350 – Software Design Singleton – Chapter 21
Basic OOP Concepts and Terms
Handling Exceptionally Sticky Problems
Introduction to Smalltalk
Presentation transcript:

Stéphane Ducasse 1 Singleton

S.Ducasse 2 Singleton’s Intent Ensure that a class has only one instance, and provide a global point of access to it

S.Ducasse 3 Problem / Solution Problem: We want a class with a unique instance. Solution: Store the first time an instance is created and return it each time a new instance is requested.

S.Ducasse 4 Example db := DBConnect default. db2 := DBConnect default. db2 == db2 > true Yes we get only one instance

S.Ducasse 5 Example Object subclass: #DBConnect instanceVariableNames: '' classVariableNames: 'UniqueInstance' poolDictionaries: '' category: 'MyDB'

S.Ducasse 6 Implementation NetworkManager class>>new self error: ‘should use uniqueInstance’ NetworkManager class>>uniqueInstance UniqueInstance isNil ifTrue: [ UniqueInstance := self basicNew initialize]. ^UniqueInstance

S.Ducasse 7 Implementation Issues Singletons may be accessed via a global variable Pharo 1.1 Smalltalk points to SmalltalkImage current Global access is good to get shorter expression Smalltalk vs SmalltalkImage current But this should be more the exception than the rule

S.Ducasse 8 Global Variable or Class Method Access Global Variable Access is dangerous: if we reassign Smalltalk we lose all references to the current SmalltalkImage Class Method Access is better because it provides a single access point. This class is responsible for the singleton instance (creation, initialization,...).

S.Ducasse 9 Implementation Issues Persistent Singleton: only one instance exists and its identity does not change Transient Singleton: only one instance exists at any time, but that instance changes Single Active Instance Singleton: a single instance is active at any point in time, but other dormant instances may also exist.

S.Ducasse 10 classVariable or class instance variable classVariable (shared variable) One singleton for a complete hierarchy Class instance variable One singleton per class

S.Ducasse 11 Access? In Smalltalk we cannot prevent a client to send a message (protected in C++). To prevent additional creation we can redefine new/new: DBConnect class>>new self error: ‘Class ‘, self name, ‘ cannot create new instances’ 11

S.Ducasse 12 Access using new: not good idea DBConnect class>>new ^self uniqueInstance The intent (uniqueness) is not clear anymore! new is normally used to return newly created instances. Not intention revealing

S.Ducasse 13 new vs uniqueInstance The difference between #new and #uniqueInstance is that #new potentially initializes a new instance, while #uniqueInstance only returns the unique instance (there is no initialization) Do we want to communicate that the class has a singleton? new? defaultInstance? uniqueInstance?

S.Ducasse 14 Favor Instance Behavior When a class should only have one instance, it is tempting to define all its behavior at the class level. This is not optimal: Class behavior represents behavior of classes: “Ordinary objects are used to model the real world. MetaObjects describe these ordinary objects” Now yes this may be tedious to type DBConnect current reconnect instead of DBConnect reconnect What’s happens if later on an object can have multiple instances? You have to change a lot of client code!

S.Ducasse 15 Time and not Scope Singleton is about time not access time: only one instance is available at the same time access: can’t you add an instance to refer to the object? Singleton for access are as bad as global variables Often we can avoid singleton by passing/referring to the object instead of favoring a global access point It is worth to have one extra instance variable that refers to the right object