PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

Prototype8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
IEG3080 Tutorial 7 Prepared by Ryan.
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.
DESIGN PATTERNS Redesigning Applications And
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Katie C. O’Shea Dennis T. Tillman 11 February 2K2 Flyweight.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
1 1.Introduction 2.Factory Method 3.Abstract Factory 4.Prototype 5.Builder 6.Singleton.
Design Patterns Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
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.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
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.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Creational Patterns
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Proxy.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
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 Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
COMPOSITE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
The Prototype Pattern (Creational) ©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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
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.
Generator Design Patterns: Singleton and Prototype
Chapter 10 Design Patterns.
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
How to be a Good Developer
object oriented Principles of software design
How to be a Good Developer
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
Informatics 122 Software Design II
Object Oriented Design Patterns - Creational Patterns
Informatics 122 Software Design II
Chapter 8, Design Patterns Singleton
Presentation transcript:

PROTOTYPE

Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory Builder Prototype Singleton Adapter Bridge Composite Facade Flyweight Proxy Chain of Responsibility Command Iterator Mediator Memento State Strategy Visitor Observer

Intent  Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Applicability  Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and  when the classes to instantiate are specified at run-time;  when instances of a class can have one of only a few different combinations of state.  It may be easier to have the proper number of prototypes and clone them rather than instantiating the class manually each time

Structure

Implementation/Sample Code class Prototype { public Prototype clone() { code to make a copy of current Prototype object return clone; } // add what ever else you want the class to do } class Protoplasm extends Prototype { public Prototype clone() { code to make a copy of current Protoplasm object return clone; } // add more other stuff } ClientCodeMethod( Prototype example ) { Prototype myCopy = example.clone(); // do some work using myCopy }

How to in Java - Object clone() protected Object clone() throws CloneNotSupportedException  Creates a clone of the object. A new instance is allocated and a bitwise clone of the current object is placed in the new object.  Returns:  a clone of this Object.  Throws: OutOfMemoryError  If there is not enough memory.  Throws: CloneNotSupportedException  Object explicitly does not want to be cloned, or it does not support the Cloneable interface.

How to in Java - Object clone() class Door implements Cloneable { Room room1; Room room2; public void Initialize( Room a, Room b) { room1 = a; room2 = b; } public Object clone() throws CloneNotSupportedException { return super.clone(); }

Shallow Copy Verse Deep Copy  Original Objects  Shallow Copy

Shallow Copy Verse Deep Copy  Original Objects  Deep Copy

Example of Prototype JavaServer handles the network connection. Algorithm: 1.Wait for client to connect 2.Open connection 3.Create IO streams to client 4.Create Server Engine to handle request 5.Let engine handle the request Variations: JavaServer will want to multiple copies of server engine to handle more than one client at a time Would like to write JavaServer just once and use it for all possible different server engines

JavaSever With Factory Method For a DateServer class JavaServer { ServerSocket acceptor; public JavaServer( int portNumber ) { acceptor = new ServerSocket( portNumber ); } public void run() { while (true) { Socket client = acceptor.accept(); InputStream cin = client.getInputStream(); OutputStream cout = client.getOutputStream(); processClientRequest( cin, cout ); } private void processClientRequest( InputStream cin, OutputStream cout ) { DateServer handler = new DateServer( cin, cout); handler.processClientRequest(); }

JavaServer …. class AirlineReservationJavaServer extends JavaServer { public AirlineReservationServer( int portNumber ) { super( portNumber ); } private void processClientRequest( InputStream cin, OutputStream cout ) { AirlineReservation handler; handler = new AirlineReservation( cin, cout ); handler.processClientRequest(); }

Design Principle 1  Program to an interface, not an implementation  Use abstract classes (and/or interfaces in Java) to define common interfaces for a set of classes  Declare variables to be instances of the abstract class not instances of particular classes

The Interface – without prototype interface ServerEngine { public void processClientRequest(InputStream in, OutputStream out); }

JavaServer with Factory Method abstract class JavaServer { ServerSocket acceptor; public JavaServer( int portNumber ) { acceptor = new ServerSocket( portNumber ); } public void run() { while (true) { Socket client = acceptor.accept(); InputStream cin = client.getInputStream(); OutputStream cout = client.getOutputStream(); ServerEngine requestHandler = makeServerEngine(); requestHandler.processClientRequest( cin, cout ); } abstract protected ServerEngine makeServerEngine(); }

JavaServer with Factory Method class AirlineReservationJavaServer extends JavaServer { public AirlineReservationServer( int portNumber ) { super( portNumber ); } protected ServerEngine makeServerEngine() { return new AirlineReservation( ); } class AirlineReservation implements ServerEngine { public void processClientRequest(InputStream in, OutputStream out) { //blah } //etc. }

JavaSever With Interface and Prototype For Any Server interface ServerEngine { public ServerEngine clone( InputStream in, OutputStream out ); public void processClientRequest(); }

JavaSever With Interface and Prototype For Any Server class JavaServer { ServerSocket acceptor; ServerEngine serverPrototype; public JavaServer( int portNumber, ServerEngine aCopy) { acceptor = new ServerSocket( portNumber ); serverPrototype = aCopy; } public void run() { while (true) { Socket client = acceptor.accept(); InputStream cin = client.getInputStream(); OutputStream cout = client.getOutputStream(); ServerEngine requestHandler = serverPrototype.clone( cin, cout); requestHandler.processClientRequest( cin, cout ); }

Driver Program class DriverProgram { public static void main( String args[] ) { ServerEngine aPrototype = new DateServer(); JavaServer networkListener; networkListener = new JavaServer( 6666, aPrototype ); networkListener.run(); }

Consequences  Adding and removing products at run-time  Specifying new objects by varying values  Specifying new objects by varying structure  Configuring an application with classes dynamically

Implementation Issues  Using a prototype manager  Implementing the Clone operation  Initializing clones