Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.

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.
Chapter 5: The Singleton Pattern
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Plab – Tirgul 12 Design Patterns
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics.
Automatically Extracting and Verifying Design Patterns in Java Code James Norris Ruchika Agrawal Computer Science Department Stanford University {jcn,
Inheritance and Polymorphism CS351 – Programming Paradigms.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Design Patterns Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
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.
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Template Design Pattern Kalim Baig. Summary What is Template? What is Template? Definition Definition Problem Problem How might it help the designer How.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
Computing IV Singleton Pattern Xinwen Fu.
Patterns COM379 University of Sunderland James Malone.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Singleton Pattern Presented By:- Navaneet Kumar ise
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Lecture 2 Intro. To Software Engineering and Object-Oriented Programming (2/2)
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.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Design Patterns: MORE Examples
Abstract Factory Pattern
Strategy Design Pattern
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Introduction to Design Patterns
Behavioral Design Patterns
Software Design and Architecture
OOP What is problem? Solution? OOP
Abstract Factory Pattern
Object Oriented Analysis and Design
Software Engineering Lecture 7 - Design Patterns
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton design pattern
CS 350 – Software Design Singleton – Chapter 21
Structural Patterns: Adapter and Bridge
Design pattern Lecture 6.
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Patterns Lecture 2

Singleton Ensure a class only has one instance, and provide a global point of access to it.

UML

Motivation Sometimes we need to ensure that there can only be one instance of an object within a given system –Filesystem acesss –Print Spooler –Thread manager –System Dictionary

Strategy The Singleton works its magic by accomplishing two things simultaneously: –hiding the class’s constructor from public view this disables the ability for others to create new instances of the class –maintaining a single reference to a single instance of the class, internally in the class

Benefits Controlled access to a single instance Enables an application to avoid the use of global references for single instances Provides a structure for the ability to have “Dual” or “Triad” objects (an object can control precisely two or three instances, and round-robin the incoming access to these objects Avoids the use of static class operations, which cannot be used polymorphically, because there is not object in play (no self or this pointer)

Java Code public class Singleton { protected Singleton(){} public static Singleton getInstance(){ if (instance == null) { instance = new Singleton(); } return instance; } private static Singleton instance = null; }

Examples Java C++ Smalltalk

Template Method Define the skeleton of an algorithm in an operation, deferring some steps to subclasses, letting subclasses define certain steps of an overall algorithm without changing the algorithm’s structure

UML

Motivation Sometimes, an algorithm is generic among multiple subtypes, with just a few exceptions These exceptions prevent the abstraction of the algorithm into a common base class Use the Template Method pattern to allow the invariant parts of the algorithm to exist in the common base class, and depend on derivatives to tailor the changeable parts of the otherwise common algorithm

Benefits Because much of the algorithm can be encapsulated in an abstract base class, the invariant parts of the algorithm are localized and non-redundant Individual behaviors are easily accomplished and themselves encapsulated in derivative classes Predefined “hook” callback methods can be assigned at defined points in a sequence of activities

Example: Insurance Policy Rating Engine Java C++ Smalltalk Common Lisp Object System (CLOS)

Bridge Decouple an abstraction from its implementation so that the two can vary independently

UML

Motivation Inheritance statically binds a particular abstraction (interface) with a particular implementation A bridge is used when an abstraction can have one of several possible implementations, but you want to be able to choose dynamically which implementation is used

Benefits Using a bridge prevents a proliferation of inheritance-based classes (classic example of multiple window platform support) Using a bridge allows you to avoid a permanent binding between an abstraction and its implementation A bridge allows you to share a single implementation amoung multiple objects, so that the client is unaware of such sharing

Examples Java