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

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
Lecture 14 Singleton pattern Fiaspel class diagram, sample code Course analysis.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Design Patterns.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Computing IV Singleton Pattern Xinwen Fu.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Object-Oriented Programming Chapter Chapter
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
ISBN Object-Oriented Programming Chapter Chapter
Introduction to Object-Oriented Programming Lesson 2.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
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.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Modern Programming Tools And Techniques-I
Design Patterns: Brief Examples
OOP: Encapsulation &Abstraction
Factory Patterns 1.
More Design Patterns 1.
More Design Patterns 1.
Software Engineering Lecture 7 - Design Patterns
More Object-Oriented Programming
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
CS 350 – Software Design Singleton – Chapter 21
Design pattern Lecture 6.
Creational Patterns.
Lecture 10 Concepts of Programming Languages
Presentation transcript:

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

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 2 Introduction A singleton can be used l when there needs to be exactly one instance of a class –a printer spooler –a system manager? l and this one instance should be globally known

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 3 Singleton structure

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 4 Consequences l Controlled access to sole instance. l Reduced name space l Permits refinement of operations and representation. l Permits a variable number of instances. l More flexible than class operations.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 5 Controlled access to sole instance l 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.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 6 Reduced name space l 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.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 7 Permits refinement of operations and representation l 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.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 8 Permits a variable number of instances l 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.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 9 More flexible than class operations l 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.

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 10 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

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 11 Implementation in C++ l The corresponding implementation is Singleton* Singleton::_instance = 0; Singleton* Singleton::instance () { if (_instance == 0) { _instance = new Singleton; } return _instance; } // instance()

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 12 Implementation in Java class 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

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 13 Candidate for use

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 14 Motivations l The setup functions do not naturally belong to any of the defined classes –define a separate manager class l acts as a ”global” function pool –it does not necessarily need to be a singleton l if several systems, each one could have its own l Management functions are needed from several components

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 15 Possible solution

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 16 Or more elaborate

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 17 The abstration levels l A measurement domain is a conceptual kind of measurement –length, weight, currency l A measurement system is a setup of measurement units –metric system: km, m, cm, mm.. –US system: mile, yard, foot, inch

7/16/2015 Singleton creational design pattern Computer Science, Karlstad University 18 Different managers manage different parts of the system l ConverterManager dynamically creates and configures the overall system components l DomainManager dynamically creates and configures the measurement systems within a domain l SystemManager dynamically creates and configures the UI components for a system