7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer...

18
03/21/22 Singleton creational de sign pattern 1 Singleton Singleton creational design creational design pattern pattern Eivind J. Nordby Karlstad University Dept. of Computer Science
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    212
  • download

    0

Transcript of 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer...

Page 1: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

04/19/23 Singleton creational design pattern

1

Singleton creational Singleton creational design patterndesign pattern

Eivind J. Nordby

Karlstad University

Dept. of Computer Science

Page 2: 7/16/2015Singleton creational design pattern1 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

Page 3: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

04/19/23Singleton creational design pattern

Computer Science, Karlstad University

3

Singleton structureSingleton structure

Page 4: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 5: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 6: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 7: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 8: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 9: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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.

Page 10: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 11: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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()

Page 12: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 13: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 14: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 15: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 16: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 17: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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

Page 18: 7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.

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