Enforcing Singularity

Similar documents
05. SINGLETON PATTERN. One of a Kind Objects

Singleton Pattern Creational

CS 351 Design of Large Programs Singleton Pattern

The Singleton Pattern. Design Patterns In Java Bob Tarr

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Introduction to Design Patterns

Introduction to Design Patterns

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Lecture 13: Design Patterns

The Singleton Pattern. Design Patterns In Java Bob Tarr

JAVA MOCK TEST JAVA MOCK TEST II

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

MSC07-J. Prevent multiple instantiations of singleton objects

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt.

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt.

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt.

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Software Design COSC 4353/6353 D R. R A J S I N G H

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Object Oriented Design

Written by John Bell for CS 342, Spring 2018

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

1 Shyam sir JAVA Notes

Finding Concurrency Bugs in Java

Modeling Dynamic Behavior

Software Engineering Design & Construction

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS342: Software Design. November 21, 2017

Design Pattern and Software Architecture: IV. Design Pattern

Selected Java Topics

The Java Memory Model

Software Engineering Design & Construction Dr. Michael Eichberg Fachgebiet Softwaretechnik Technische Universität Darmstadt

Java Threads and intrinsic locks

JAVA MOCK TEST JAVA MOCK TEST IV

JAVA MOCK TEST JAVA MOCK TEST III

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Name of subject: JAVA PROGRAMMING Subject code: Semester: V ASSIGNMENT 1

Chapter 5 Object-Oriented Programming

Modeling Dynamic Behavior

An Introduction to Patterns

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Module 10 Inheritance, Virtual Functions, and Polymorphism

5/23/2015. Core Java Syllabus. VikRam ShaRma

Introduction to Software Engineering (2+1 SWS) Winter Term 2009 / 2010 Dr. Michael Eichberg Vertretungsprofessur Software Engineering Department of

Software Engineering Design & Construction

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Chapter 14 Abstract Classes and Interfaces

15CS45 : OBJECT ORIENTED CONCEPTS

LOG6306 : Études empiriques sur les patrons logiciels

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 9.

The Composite Design Pattern

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Advanced concurrent programming in Java Shared objects

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Special Topics: Programming Languages

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Design Patterns Revisited

What is a thread anyway?

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

C++ (Non for C Programmer) (BT307) 40 Hours

Design Pattern- Creational pattern 2015

Object Oriented Programming. Java-Lecture 11 Polymorphism

Synchronization SPL/2010 SPL/20 1

Software Engineering Design & Construction

Chapter 2: Java OOP I

1B1b Classes in Java Part I

JOURNAL OF OBJECT TECHNOLOGY

The Observer Design Pattern

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

What is Design Patterns?

Java Overview An introduction to the Java Programming Language

Java Object Oriented Design. CSC207 Fall 2014

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Overview of Java s Support for Polymorphism

Inheritance (Part 2) Notes Chapter 6

Automatic Testing of Sequential and Concurrent Substitutability

Object-Oriented Design

Patterns Continued and Concluded. July 26, 2017

D Programming Language

CS263: Runtime Systems Lecture: High-level language virtual machines

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

Self-review Questions

An Introduction to Patterns

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

20 Most Important Java Programming Interview Questions. Powered by

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Introduction to Programming Using Java (98-388)

Java and C CSE 351 Spring

COMP322 - Introduction to C++ Lecture 09 - Inheritance continued

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Transcription:

Dr. Michael Eichberg Software Engineering Department of Computer Science Technische Universität Darmstadt Software Engineering Enforcing Singularity For details see Gamma et al. in Design Patterns

Enforcing Singularity Example / Motivation 2 In some cases a mechanism is required to enforce singularity of objects; i.e. it is necessary to enforce that there exists at most one instance of a class at runtime. For example, in a system there should be only one printer spooler, there should be only one class to handle interactions with the database. Two patterns for enforcing Singularity: Singleton Monostate

Enforcing Singularity Example / Motivation 3 In some cases a mechanism is required to enforce singularity of objects; i.e. it is necessary to enforce that there exists at most one instance of a class at runtime. For example, in a system Beware: there should Often be it is only best one to just printer create spooler, one there should instance be only of an one object class (using to handle the constructor) interactions with the at program initialization time and to use this database. object. Two patterns for enforcing Singularity: Singleton Monostate

The Singleton Design Pattern Intent and Structure 4 Intent Ensure a class only has one instance, and provide a global point of access to it. Structure «method» { return uniqueinstance; } Singleton - static uniqueinstance static instance()

The Singleton Design Pattern Implementation 5 «method» { return uniqueinstance; } Singleton - static uniqueinstance static instance() public class Singleton { private static Singleton theinstance = null; private Singleton(); The constructor should be private or protected. } public static Singleton instance() { if (theinstance == null) theinstance = new Singleton(); return theinstance; } The implementation is not thread safe. (Recall the discussion of the double-checked locking idiom.)

The Singleton Design Pattern Benefits Costs 6 Cross platform Using appropriate middleware, Singleton can be extended to work across many JVMs. Applicable to any class Can be created through derivation Given a class, you can create a subclass that is a Singleton. Lazy creation (Controlled access to sole instance.) If the singleton is never used, it is never created. Destruction is undefined Not inherited A class derived from a singleton is not a singleton. Nontransparent Users of a Singleton know that they are using a Singleton.

7 Software design patterns capture tried and successful design solutions. Among different views on design patterns is that they are created to compensate for the design shortfalls in programming languages - that is, design patterns are needed when programming languages cannot do the job in a straightforward way. Liping Zhao; Patterns, Symmetry, and Symmetry Breaking; Communications of the ACM, March 2008, Vol. 51, No. 3

Patterns as a motivation for language features. Singleton objects in the Scala Programming Language (http://www.scala-lang.org/) The same program implemented in two programming languages. 8 1 object Hello { 2 // methods 3 } 1 public class Hello { 2 3 private static Hello hello; 4 5 private Hello(){ } 6 7 public static synchronized Hello instance() { 8 if (hello == null) { 9 hello = new Hello(); 10 } 11 return hello; 12 } 13 14 // methods 15 } Java

The Monostate Design Pattern Intent and Implementation Agile Software Development - Principles, Patterns and Practices 9 Intent Make all objects of the same type behave as though they were a single object Implementation Make all fields static Methods are not static public class X { public X(){ } private static boolean x = ; } public boolean isx() { return x; }

The Monostate Design Pattern Benefits Costs 10 Transparent The user does not need to know that the object is Monostate. Destruction is well-defined. Derivability Derivatives of a Monostate are Monostates; derivatives of a Monostate are part of the same Monostate. Polymorphism Since the methods of a Monostate are not static, they can be overridden in a derivative. Derivatives can offer different behavior over the same set of static variables. No conversion A normal class cannot be converted into a Monostate. Efficiency A Monostate may go through many creations and destructions because it is a real object. Presence The variables of a Monostate take up space, even if the Monostate is never used. Platform local A Monostate cannot work across several JVM instances or across several platforms.

Singleton vs. Monostate Design Pattern Design Patterns 11 Singleton is best used when you have an existing class that you want to constrain through der-ivation, and you don t mind that everyone will have to call the instance() method to gain ac-cess. Monostate is best used when you want the singular nature of the class to be transparent to the users, or w h e n y o u w a n t t o e m p l o y polymorphic derivatives of the single object. Singleton is about structure! Monostate is about behavior!