Creational Pattern: Prototype

Slides:



Advertisements
Similar presentations
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Advertisements

EC-241 Object-Oriented Programming
C++ Classes & Data Abstraction
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1 Spring 2012.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
C++ fundamentals.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
In the name of Allah The Proxy Pattern Elham moazzen.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Computing IV Singleton Pattern Xinwen Fu.
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.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Design Patterns Solving problems with already known solutions Unit - 13.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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.
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Overview of C++ Polymorphism
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
1 Introduction to Object Oriented Programming Chapter 10.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 10 Advanced Topics.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Creational Patterns C h a p t e r 3 – P a g e 14 Creational Patterns Design patterns that deal with object creation mechanisms and class instantiation,
Modern Programming Tools And Techniques-I
Factory Method Pattern
University of Central Florida COP 3330 Object Oriented Programming
CS 3370 – C++ Object-oriented Programming
Software Design and Architecture
Review: Two Programming Paradigms
C++ Classes & Object Oriented Programming
Introduction to Classes
Chapter 5 Classes.
CMPE 135: Object-Oriented Analysis and Design October 17 Class Meeting
University of Central Florida COP 3330 Object Oriented Programming
Factory Method Pattern
C++ History C++ was designed at AT&T Bell Labs by Bjarne Stroustrup in the early 80's Based on the ‘C’ programming language C++ language standardised in.
Finally! Discussing a language!
The dirty secrets of objects
Polymorphism Lec
Object Oriented Analysis and Design
Advanced Programming Behnam Hatami Fall 2017.
Object Based Programming
Introduction to Classes
Java Programming Language
Prototype Pattern 1.
Object Oriented Programming
Inheritance: Polymorphism and Virtual Functions
Lesson 5: More on Creational Patterns
Fundaments of Game Design
Overview of C++ Polymorphism
Java Programming Language
CS410 – Software Engineering Lecture #8: Advanced C++ III
Presentation transcript:

Creational Pattern: Prototype Chapter 3 – Page 37 While the Factory Method pattern lets a class defer instantiation to its subclasses, the Prototype pattern uses delegation to accomplish the same task. Rather than undergo the built-in cost of generating new objects whenever they need to be created, a prototype of the desired object is “cloned” and modified as desired. The prototypes themselves are often created prior to run-time, with their cloned variations added at run-time.

The Prototype Pattern Chapter 3 – Page 38 The Prototype declares an interface for cloning itself. Each ConcretePrototype implements an operation for cloning itself. The Client creates a new object by asking the Prototype to clone itself. Note that the Prototype pattern is unique among the Creational Design Patterns in that it doesn’t actually require a class; some pure object-oriented languages (e.g., Self, Omega) do away with classes entirely and rely exclusively on prototypes to create new objects.

Non-Software Example Chapter 3 – Page 39 Assume that an e-commerce application gathers product information via complicated queries against a legacy database, which is updated at a known set of predefined intervals. The application could instantiate the product information (via cloning) at these predefined intervals and keep it in a cache. When a user requests information about a certain product, it is retrieved from the cache and cloned. When the legacy database is updated, the contents of the cache are discarded and reloaded with new objects.

Prototype Example: Images Chapter 3 – Page 40 The client is using two types of satellite images, the American LandSat images and the European SPOT images, implemented as subclasses of an Image prototype. Whenever the client needs a clone of a particular type, the clone() operator is called on the correct derived class.

C++ Code for Image Prototype Chapter 3 – Page 41 #include <iostream> using namespace std; enum imageType { LSAT, SPOT }; class Image { public: virtual void draw() = 0; static Image *findAndClone(imageType); protected: virtual imageType returnType() = 0; virtual Image *clone() = 0; // As each subclass of Image is declared, it registers its prototype static void addPrototype(Image *image) { prototypes[nextSlot++] = image; } private: // addPrototype() saves each registered prototype here static Image *prototypes[10]; static int nextSlot; }; Image *Image::prototypes[]; int Image::nextSlot; // Client calls this public static member function // when it needs an instance of an Image subclass Image *Image::findAndClone(imageType type) for (int i = 0; i < nextSlot; i++) if (prototypes[i]->returnType() == type) return prototypes[i]->clone(); return NULL; }

Chapter 3 – Page 42 class LandSatImage: public Image { public: imageType returnType() { return LSAT; } void draw() { cout << "LandSatImage::draw " << id << endl; } // When clone() is called, call the one-argument // constructor with a dummy arg Image *clone() { return new LandSatImage(1); } protected: // This is only called from clone() LandSatImage(int dummy) { id = count++; } private: // Mechanism for initializing an Image subclass - this causes the default // constructor to be called, which registers the subclass's prototype static LandSatImage landSatImage; // This is only called when the private static data member is inited LandSatImage() { addPrototype(this); } // Nominal "state" per instance mechanism int id; static int count; }; // Register the subclass's prototype LandSatImage LandSatImage::landSatImage; // Initialize the "state" per instance mechanism int LandSatImage::count = 1;

Chapter 3 – Page 43 class SpotImage: public Image { public: imageType returnType() { return SPOT; } void draw() { cout << "SpotImage::draw " << id << endl; } Image *clone() { return new SpotImage(1); } protected: SpotImage(int dummy) { id = count++; } private: SpotImage() { addPrototype(this); } static SpotImage spotImage; int id; static int count; }; SpotImage SpotImage::spotImage; int SpotImage::count = 1; // Simulated stream of creation requests const int NUM_IMAGES = 8; imageType input[NUM_IMAGES] = { LSAT, LSAT, LSAT, SPOT, LSAT, SPOT, SPOT, LSAT }; void main() Image *images[NUM_IMAGES]; int i; // Given an image type, find the right prototype, and return a clone for (i = 0; i < NUM_IMAGES; i++) images[i] = Image::findAndClone(input[i]); // Demonstrate that correct image objects have been cloned images[i]->draw(); // Free the dynamic memory delete images[i]; } Chapter 3 – Page 43

Prototype Design Advantages Chapter 3 – Page 44 The Prototype pattern hides the complexities of making new instances from the client, which can be very helpful when there is a complex class hierarchy and new objects of many different types need to be created. If the objects are complicated enough, copying an object can be more efficient that creating a new one. In addition to the inheritance-vs.-delegation difference, the Prototype pattern differs from the Factory Method pattern in that Prototype doesn’t require subclasses but does require initialization, while Factory Method doesn’t require initialization but does require subclasses.