7/16/2015singleton creational design pattern1 eivind j. nordby karlstad university dept. of computer...
Post on 22-Dec-2015
212 views
TRANSCRIPT
04/19/23 Singleton creational design pattern
1
Singleton creational Singleton creational design patterndesign pattern
Eivind J. Nordby
Karlstad University
Dept. of Computer Science
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
2
Introduction Introduction
A singleton can be used when there needs to be exactly one instance
of a class– a printer spooler– a system manager?
and this one instance should be globally known
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
3
Singleton structureSingleton structure
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
4
ConsequencesConsequences
Controlled access to sole instance. Reduced name space Permits refinement of operations and
representation. Permits a variable number of instances. More flexible than class operations.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
5
Controlled access to sole Controlled access to sole instanceinstance
Controlled access to sole instance.– Because the Singleton class encapsulates its
sole instance, it can have strict control over how and when clients access it.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
6
Reduced name spaceReduced name space
Reduced name space.– The Singleton pattern is an improvement over
global variables. It avoids polluting the name space with global variables that store sole instances.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
7
Permits refinement of Permits refinement of operations and representationoperations and representation
Permits refinement of operations and representation.– The Singleton class may be subclassed, and it's
easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
8
Permits a variable number Permits a variable number of instancesof instances
Permits a variable number of instances.– The pattern makes it easy to change your mind
and allow more than one instance of the Singleton class. Moreover, you can use the same approach to control the number of instances that the application uses. Only the operation that grants access to the Singleton instance needs to change.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
9
More flexible than class More flexible than class operationsoperations
More flexible than class operations.– Another way to package a singleton's
functionality is to use class operations (that is, static member functions in C++ or class methods in Smalltalk). But both of these language techniques make it hard to change a design to allow more than one instance of a class. Moreover, static member functions in C++ are never virtual, so subclasses can't override them polymorphically.
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
10
Declaration in C++Declaration in C++
The Singleton class is declared as
class Singleton { public: static Singleton* instance(); protected: Singleton(); public:
// operations for the singleton ... private: static Singleton* _instance;
}; // class Singleton
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
11
Implementation in C++Implementation in C++
The corresponding implementation is
Singleton* Singleton::_instance = 0;
Singleton* Singleton::instance () { if (_instance == 0) { _instance = new Singleton; } return _instance; } // instance()
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
12
Implementation in JavaImplementation in Javaclass Singleton { private static Singleton _instance = null;
protected Singleton() {}
public static Singleton instance() { if (_instance == null) { _instance = new Singleton(); } return _instance; } // instance
// operations for the singleton ...}; // class Singleton
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
13
Candidate for useCandidate for use
DomainModel
addObserver(Observer)
setValue(double)getValue()
components
SystemModel
update(Observable,Object)
itemStateChanged()actionPerformed() adjustableValueChanged()
units
controller
Choice
getSelectedIndex()unitChooser
itemListener
Converter
value)
observers
SystemViewcomponents
adaptee
TextField orScrollbar
setText(…) orsetValue(…)
Observer-Adapter
update()
listener
subject
observers
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
14
MotivationsMotivations
The setup functions do not naturally belong to any of the defined classes– define a separate manager class
acts as a ”global” function pool
– it does not necessarily need to be a singleton if several systems, each one could have its own
Management functions are needed from several components
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
15
Possible solutionPossible solution
«Observable»DomainModel «Observer»
«Observable»SystemModel
Choice
DomainView SystemView
TextField orScrollbar
«Adapter»Observer-Adapter
Converter «Singleton»DomainManager
«Singleton»SystemManager
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
16
Or more elaborateOr more elaborate
«Observable»DomainModel «Observer»
«Observable»SystemModel
DomainView SystemView
TextField,Scrollbar or
Choice
«Adapter»Observer-Adapter
Converter mainprogram or browser /
applet viewer
«Singleton»DomainManager
«Singleton»SystemManager
«Singleton»ConverterManager
«Adapter»Listener-Adapter
ConverterView
Unit
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
17
The abstration levelsThe abstration levels
A measurement domain is a conceptual kind of measurement– length, weight, currency
A measurement system is a setup of measurement units– metric system: km, m, cm, mm ..– US system: mile, yard, foot, inch
04/19/23Singleton creational design pattern
Computer Science, Karlstad University
18
Different managers manage Different managers manage different parts of the systemdifferent parts of the system
ConverterManager dynamically creates and configures the overall system components
DomainManager dynamically creates and configures the measurement systems within a domain
SystemManager dynamically creates and configures the UI components for a system