Prototype | slide # 2 |
...Intent | slide # 2 |
...Applicability | slide # 2 |
...Implementation/Sample Code | slide # 3 |
...Cloning Issues | slide # 4 |
......How to in C++ - Copy Constructors | slide # 4 |
......How to in Java - Object clone() | slide # 5 |
......Shallow Copy Verse Deep Copy | slide # 6 |
...Example of Prototype | slide # 8 |
...Consequences | slide # 15 |
...Implementation Issues | slide # 15 |
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 }
class Door { public: Door(); Door( const Door&); virtual Door* clone() const; virtual void Initialize( Room*, Room* ); // stuff not shown private: Room* room1; Room* room2; } Door::Door ( const Door& other ) //Copy constructor { room1 = other.room1; room2 = other.room2; } Door* Door::clone() const { return new Door( *this ); }
Creates a clone of the object. A new instance is allocated and a bitwise clone of the current object is place in the new object.
Returns:
class Door implements Cloneable { public void Initialize( Room a, Room b) { room1 = a; room2 = b; } public Object clone() throws CloneNotSupportedException { return super.clone(); } Room room1; Room room2; }
JavaServer handles the network connection.
Algorithm:
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(); } }
class AirlineReservationJavaServer extends JavaServer { ServerSocket acceptor; public AirlineReservationServer( int portNumber ) { super( portNumber ); } private void processClientRequest( InputStream cin, OutputStream cout ) AirlineReservation handler; handler = new AirlineReservation( cin, cout ); handler.processClientRequest(); } }
Declare variables to be instances of the abstract class not instances of particular classes
interface ServerEngine { public ServerEngine clone( InputStream in, OutputStream out ); public void processClientRequest(); }
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(); processClientRequest( cin, cout ); } } private void processClientRequest( InputStream cin, OutputStream cout ) { ServerEngine handler; handler = serverPrototype.clone( cin, cout); handler.processClientRequest(); } }Driver Program
class DriverProgram { public static void main( String args[] ) { ServerEngine aFactory = new DateServer(); JavaServer networkListener; networkListener = new JavaServer( 6666, aFactory ); networkListener.run(); } }