21
PROTOTYPE

PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

PROTOTYPE

Page 2: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Design Pattern SpacePurpose

Scope Creational Structural BehavioralClass Factory Method Adapter Interpreter

Template Method

Object Abstract factoryBuilderPrototypeSingleton

AdapterBridgeCompositeFacadeFlyweightProxy

Chain of ResponsibilityCommandIteratorMediatorMementoStateStrategyVisitorObserver

Page 3: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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

Page 4: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Structure

Page 5: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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 }

Page 6: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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.

Page 7: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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(); } }

Page 8: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Shallow Copy Verse Deep Copy Original Objects

Shallow Copy

Page 9: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Shallow Copy Verse Deep Copy Original Objects

Deep Copy

Page 10: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

Example of Prototype JavaServer handles the network connection.

Algorithm: 1. Wait for client to connect2. Open connection3. Create IO streams to client4. Create Server Engine to

handle request5. 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

Page 11: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

JavaSever With Factory MethodFor a DateServerclass 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(); } }

Page 12: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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(); } }

Page 13: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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

Page 14: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

The Interface – without prototypeinterface ServerEngine { public void processClientRequest(InputStream in, OutputStream

out); }

Page 15: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

JavaServer with Factory Methodabstract 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(); }

Page 16: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

JavaServer with Factory Methodclass 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. }

Page 17: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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

Page 18: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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 ); } } }

Page 19: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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

}

Page 20: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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

Page 21: PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter…

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