1 / 21

Prototype

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

cally-lowe
Download Presentation

Prototype

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prototype

  2. Design Pattern Space

  3. 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

  4. Structure

  5. 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 }

  6. 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.

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

  8. Shallow Copy Verse Deep Copy • Original Objects • Shallow Copy

  9. Shallow Copy Verse Deep Copy • Original Objects • Deep Copy

  10. 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

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

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

  13. 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

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

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

  16. 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. }

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

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

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

  20. 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

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

More Related