March 200692.3913 Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.

Slides:



Advertisements
Similar presentations
Factory Pattern Building Complex Objects. New is an implementation  Calling “new” is certainly coding to an implementation  In fact, it’s always related.
Advertisements

Chapter 4: The Factory Pattern. Consider the Following Code Fragment Duck duck; if (picnic) { duck = new MallardDuck(); } else if (hunting) { duck = new.
CS 210 Introduction to Design Patterns September 19 th, 2006.
Command Pattern 1. Intent Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log request,
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
March Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Jan Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Feb Ron McFadyen1 Iterator Pattern Recall Generic UML class diagram The iterator is used to access the elements of some aggregate. The aggregate.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Informatics 122 Software Design II Lecture 5 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Feb Ron McFadyen1 Factory Method Iterator Example : Java collection classes represent an example of the Factory Method design pattern. The.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Feb Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface the client expects. An adapter lets classes work.
Fall 2009ACS Ron McFadyen1 The context maintains an instance of a concrete state subclass State Pattern Each subclass (concrete state) implements.
March Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.
Command Pattern Chihung Liao Cynthia Jiang. Waiter Order Execute() Hamburger Execute() Hot Dogs Execute() Fries Execute() Cook Make Food()
Spring 2010ACS-3913 Ron McFadyen1 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Fall 2009ACS-3913 R McFadyen1 Singleton Problem: Exactly one instance of a certain object is required (this object is called a singleton). We must ensure.
Spring 2010ACS-3913 Ron McFadyen1 Duck Example Consider the text example (up to page 6). Each type of duck is a subclass of Duck Most subclasses implement.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
+ Informatics 122 Software Design II Lecture 8 Emily Navarro Duplication of course material for any commercial purpose without the explicit written permission.
Design Patterns.
CS 210 Introduction to Design Patterns September 28 th, 2006.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Abstract Factory Abstract Factory using Factory Method.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns Façade, Singleton, and Factory Methods Team Good Vibrations (1)
Patterns COM379 University of Sunderland James Malone.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
CS 210 Review Session October 5 th, Head First Design Patterns Chapter 4 Factory Pattern.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Abstract Factory and Factory Method CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Design Patterns Singleton & Factory Pattern Eriq Muhammad Adams J. Mail : | Blog :
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
CSC 480 Software Engineering Design With Patterns.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns Introduction
The Factory Pattern Sanjay Yadav (ISE ).
CSC 480 Software Engineering Design With Patterns.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Singleton Pattern Presented By:- Navaneet Kumar ise
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
SE 461 Software Patterns. ABSTRACT FACTORY PATTERN.
SE 461 Software Patterns. FACTORY METHOD PATTERN.
Factory Method. Intent/Purpose Factory Method is used to deal with a problem of creating objects without specifying the EXACT class of object that we.
Factory Method Pattern. Admin SCPI Patner Day Feb. 21 Lunch count Presentation (4-8 min.) Practice on Feb. 16. Morning availablity on Feb21 Brief overview.
Design Patterns Spring 2017.
Chapter 10 Design Patterns.
Low Budget Productions, LLC
Factory Patterns 1.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Software Design and Architecture
Singleton Pattern Command Pattern
Design Patterns in Operating Systems
Programming Design Patterns
Command Pattern 1.
Software Engineering Lecture 7 - Design Patterns
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Singleton Pattern Pattern Name: Singleton Pattern Context
Singleton …there can be only one….
Presentation transcript:

March Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly one object is needed to coordinate actions across the system. Singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist.

March Ron McFadyen2 Singleton pattern If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. eager instantiation lazy instantiation - no resources until needed The singleton pattern must be carefully constructed in multi- threaded applications. getInstance() Singleton() Singleton Public Private

March Ron McFadyen3 Singleton pattern Structure? Behaviour? How would singleton combine with observer if there should only be one subject? Could singleton and decorator combine? Could the same decorated object be decorated differently for different clients?

March Ron McFadyen4 Factory method pattern We have a situation where objects (called products) are created. Products are organized into subclasses referred to as concrete products. A parallel hierarchy exists with a creator superclass organized with concrete creator subclasses. The concrete creators are responsible for instantiating concrete products. In general we say the Factory Method defers instantiation to subclasses. Factory Method defines an interface for creating an object, but let subclasses decide which class to instantiate.

March Ron McFadyen5 Factory method – generic class diagram product = factoryMethod() return product Creator Concrete Creator1 createMethod() factoryMethod() Product Concrete Product1 Concrete Product2 Concrete Creator2 factoryMethod() Client

March Ron McFadyen6 Factory method – generic class diagram Pizza = createPizza() PizzaStore NyPizza Store orderPizza() createPizza() Pizza NyCheese Pizza Chicago CheesePizza Chicago PizzaStore createPizza() main In the text example, the creator (specific pizza store) subclass is instantiated by testdrive, and then a number of products (specific pizzas) are created. The model works for whatever pizza store gets instantiated and its easy to incorporate new stores return new ChicagoCheesePizza()

March Ron McFadyen7 Factory method – generic class diagram Pizza = createPizza() PizzaStore NyPizza Store orderPizza() createPizza() Pizza NyCheese Pizza Chicago CheesePizza Chicago PizzaStore createPizza() main FactoryMethod Abstract factory Concrete factory Concrete product Abstract product

March Ron McFadyen8 Factory method pattern What is the behaviour for when main orders a Cheese Pizza when the store is NyPizzaStore?

March Ron McFadyen9 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command to perform its function and the concrete command will in turn ask some receiver to perform a pre-determined action.

March Ron McFadyen10 Command – generic class diagram > Command Concrete Command Receiver Invoker Command Concrete commandreceiver command action1() action2() execute() setCommand() invoker

March Ron McFadyen11 Command – text example > Command lightOn Command Light Remote control Command Concrete command receiver command on() off() execute() setCommand() invoker lightOff Command execute() Concrete command null Command execute() Concrete command { }

March Ron McFadyen12 Command - behaviour :Light:RemoteControl execute() lightOn() execute() lightOff() :LightOn Command :LightOff Command :null Command execute()