Generator Design Patterns: Singleton and Prototype

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Basics Prof. Ankur Teredesai, Computer Science Department, RIT.
Advertisements

© 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 1 Generator Design Patterns: The Factory Patterns.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Copyright © Wondershare Software نام و نام ‌ خانوادگی علی عبدلی استاد درس آقای دکتر نورحسینی.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Prototype Pattern Intent:
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
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.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Advanced Programming Rabie A. Ramadan 7.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
ECE450S – Software Engineering II
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Creational Patterns
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
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.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Design Patterns By Mareck Kortylevitch and Piotreck Ratchinsky.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Singleton Pattern Presented By:- Navaneet Kumar ise
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Patterns in programming
Abstract Factory Pattern
Object-Oriented Programming Basics
MPCS – Advanced java Programming
Object-Oriented Modeling with UML
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Introduction to Design Patterns
Creational Pattern: Prototype
Creational Design Patterns
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Advanced Programming Behnam Hatami Fall 2017.
Object Oriented Design Patterns - Creational Patterns
Prototype Pattern 1.
Singleton Pattern Pattern Name: Singleton Pattern Context
CS 350 – Software Design Singleton – Chapter 21
Lesson 5: More on Creational Patterns
5. Strategy, Singleton Patterns
Presentation transcript:

Generator Design Patterns: Singleton and Prototype

Objectives To introduce singletons and the Singleton pattern To introduce cloning and discuss problems about shallow and deep copies of cloned objects To introduce the Prototype pattern

Topics Singletons The Singleton pattern Cloning Shallow and deep copies The Prototype pattern

A singleton is a class that can have only one instance. Singletons Often there is a need to guarantee that a class has only a single (or a few) instances. Servers Controllers Managers A singleton is a class that can have only one instance.

The Singleton Pattern Guarantees that a class is a singleton Can be modified to provide any set number of instances Provides wide (often global) access to the single instance Can be modified to provide more restricted access Analogy: gods of the Greek, Roman, or Norse pantheons

Singleton Pattern Structure

Singleton Pattern Behavior

Singleton Examples Examples of the need for global unique entities in a program abound: Subsystems (or their façade objects) Communication streams Major containers or aggregates OS or windowing system proxies

When to Use Singletons Use the Singleton pattern to guarantee that there is only one or a small number of instances of some class. Sometimes an abstract class with static attributes and operations can be used instead, but Often languages can’t store or pass around references to classes; Polymorphism doesn’t work with classes; This only works for singletons, not couples, etc.

A clone is a copy of an object. Cloning An alternative to class instantiation is making a copy of an object. A clone is a copy of an object. A clone has the state of its source at the moment of creation.

Shallow and Deep Copies Copying objects that hold references to other entities raises the question “Should the references or the referenced entities be copied?” Shallow copy: Copy references when an entity is copied. Deep copy: Copy referenced entities (and any entities they reference as well) when an entity is copied. Sometimes a shallow copy is the right thing to do, and sometimes a deep copy is.

Shallow vs. Deep Copy Example

Shallow or Deep Copy in Cloning? Should a clone be made using a shallow or a deep copy? It does not matter if no attributes hold references. There is not accepted practice. What ought to be done depends on the particular case at hand.

The Prototype Pattern Uses cloning implemented by a clone() factory method Uses interfaces to decouple the client from the cloned product Does not specify whether cloning is deep or shallow—use the right one for the case at hand Analogy: using an instance of something to get a new one

Prototype Pattern Structure

Prototype Pattern Behavior

Example: Graphic Prototypes

When to Use the Prototype Pattern Use the Prototype pattern when clients need to be decoupled from products, and the products need to be set at runtime. The main problem with the prototype pattern is that it relies on cloning, which presents problems about shallow and deep copies.

Summary The Singleton pattern is used to guarantee that only one or a set number of instances of some class exist. The Prototype pattern is used to create instances of classes (really copies of objects) determined at run time.