210 likes | 363 Views
Prototype. Design Pattern Space. Intent. Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype Applicability
E N D
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
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: • Wait for client to connect • Open connection • Create IO streams to client • Create Server Engine to handle request • 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 MethodFor 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