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

Similar documents
Declarations and Access Control SCJP tips

Classes, interfaces, & documentation. Review of basic building blocks

More on Objects in JAVA TM

JAVA MOCK TEST JAVA MOCK TEST II

Written by John Bell for CS 342, Spring 2018

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

CS 351 Design of Large Programs Singleton Pattern

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

1 Inheritance (8 minutes, 9 points)

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Writing your own Exceptions. How to extend Exception

Rules and syntax for inheritance. The boring stuff

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Dealing with Bugs. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 27 04/21/2009

TaxiBot New attributes Variables Math! TaxiBot

Selected Java Topics

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

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

CS 231 Data Structures and Algorithms, Fall 2016

Enums. In this article from my free Java 8 course, I will talk about the enum. Enums are constant values that can never be changed.

Singleton Pattern Creational

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

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

INHERITANCE. Spring 2019

Code Reuse: Inheritance

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

Thinking Functionally

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

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

Java Review. Fundamentals of Computer Science

CSC Java Programming, Fall Java Data Types and Control Constructs

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Object Oriented Programming in C#

Object-Oriented Design. NCCU Fall Lecture 4-2: Design Patterns-I Oct

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

CSE 374 Programming Concepts & Tools

MSO Object Creation Singleton & Object Pool

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

CSE 413 Spring Introduction to Ruby. Credit: Dan Grossman, CSE341

Simple Factory Pattern

CSE341: Programming Languages Lecture 4 Records, Datatypes, Case Expressions. Dan Grossman Spring 2013

COMP 250 Winter 2011 Reading: Java background January 5, 2011

These are notes for the third lecture; if statements and loops.

And Even More and More C++ Fundamentals of Computer Science

Lecture 10 Declarations and Scope

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

Design Patterns Revisited

MSC07-J. Prevent multiple instantiations of singleton objects

Exceptions and Design

1 Shyam sir JAVA Notes

1B1b Classes in Java Part I

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

EXAM Computer Science 1 Part 1

Subclassing for ADTs Implementation

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

CS211 Computers and Programming Matthew Harris and Alexa Sharp July 9, Boggle

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

QUIZ Friends class Y;

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

CMSC 132: Object-Oriented Programming II

Prelim 1. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants

CS/ENGRD 2110 SPRING 2018

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CSE 113 A. Announcements - Lab

Definition of DJ (Diminished Java)

Queues. CITS2200 Data Structures and Algorithms. Topic 5

COP 3330 Final Exam Review

Learning to Program with Haiku

Homework #10 due Monday, April 16, 10:00 PM

MITOCW watch?v=kz7jjltq9r4

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Practice exam for CMSC131-04, Fall 2017

Program Fundamentals

Final Exam CS 251, Intermediate Programming December 10, 2014

(CONDITIONALS_BOUNDARY)

Pace University. Fundamental Concepts of CS121 1

Introduction to Programming Using Java (98-388)

6. Java Errors and Exceptions

Computer Components. Software{ User Programs. Operating System. Hardware

05. SINGLETON PATTERN. One of a Kind Objects

public class Q extends P { private int num; private int i; P(int n) { public Q() { this.num = n;

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

CS/ENGRD 2110 SPRING 2018

Array. Prepared By - Rifat Shahriyar

Model answers/marking scheme in bold, additional comments in bold italic.

ACORN.COM CS 1110 SPRING 2012: ASSIGNMENT A1

Transcription:

CMSC 433 Programming Language Technologies and Paradigms Spring 2007 Singleton Pattern Mar. 13, 2007 What is it? If you need to make sure that there can be one and only one instance of a class. For example, your system can have only one window manager or print spooler, or a single point of access to a database engine. 1 2 Approach 1 Disadvantage of Approach 1 Embed a static variable inside the class that we set on the first instance and check for each time we enter the constructor. A static variable is one for which there is only one instance, no matter how many instances there are of the class. static boolean instance_flag = false; 3 How to find out whether creating an instance was successful or not Remember constructors do not return values. One way would be to call a method that checks for the success of creation, and which simply returns some value derived from the static variable. This is inelegant and prone to error because there is nothing to keep you from creating many instances of such non-functional classes and forgetting to check for this error condition. 4

Approach 2 Why a New Exception? Create a class that throws an Exception when it is instantiated more than once. Let s create our own exception class for this case This new exception type doesn t do anything in particular other than calling its parent classes through the super() method,. However, it is convenient to have our own named exception type so that the compiler will warn us of the type of exception we must catch when we attempt to create an instance 5 6 Lets Implement the Class Lets Use it! 7 8

Disadvantage of Approach 2 Approach 3 Must enclose every method that may throw an exception in a try - catch block. And the exception-based solution is not really transparent There already is a kind of Singleton class in the standard Java class libraries: the Math class. This is a class that is declared final and all methods are declared static, meaning that the class cannot be extended. The purpose of the Math class is to wrap a number of common mathematical functions such as sin and log in a class-like structure, since the Java language does not support functions that are not methods in a class. You can t create any instance of classes like Math, and can only call the static methods directly in the existing final class. You can use the same approach to a Singleton pattern, making it a final class. 9 10 Approach 3 Disadvantage of Approach 3 Difficult to drop the restrictions of Singleton status a lot of reprogramming to do to make the static approach allow multiple instances this is easier to do in the exception style class structure 11 12

Approach 4 Approach 4 Create Singletons using a static method to issue and keep track of instances. To prevent instantiating the class more than once, make the constructor private so an instance can only be created from within the static method of the class 13 14 Approach 4 Lets Use it! Don t have to worry about exception handling if the singleton already exists-- you simply get a null return from the Instance method 15 16

Compile-time Error Checking Approach 5 should you try to create instances of the ispooler class directly, this will fail at compile time because the constructor has been declared as private. public class Singleton { // Private constructor suppresses generation // of a (public) default constructor private Singleton() { private static class SingletonHolder { private static Singleton instance = new Singleton(); public static Singleton getinstance() { return SingletonHolder.instance; 17 18 Dropping the Singleton Requirement Suddenly, we decide that we can allow n instances of the (previously) Singleton object How would you adapt each of the previous five approaches? Which implementation is maintainable? 19