SDSU CS 596: Client-Server Programming
Spring Semester, 1997
Doc 10, Prototype

To Lecture Notes Index
San Diego State University -- This page last updated Mar 10, 1997
----------

Contents of Doc 10, Prototype


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


Doc 10, Prototype Slide # 1
References

Design Patterns: Elements of Resuable Object-Oriented Software, Gamma, Helm, Johnson, Vlissides, Addison-Wesley, 1995
Doc 10, Prototype Slide # 2

Prototype

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


Doc 10, Prototype Slide # 3

Implementation/Sample Code

Simple Example

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
     }

Doc 10, Prototype Slide # 4

Cloning Issues

How to in C++ - Copy Constructors


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


Doc 10, Prototype Slide # 5

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

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

Doc 10, Prototype Slide # 6

Shallow Copy Verse Deep Copy


Original Objects

door pic


Shallow Copy

shallowCopy


Doc 10, Prototype Slide # 7
Shallow Copy Verse Deep Copy

Original Objects

door pic


Deep Copy

deep copy


Doc 10, Prototype Slide # 8

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

Doc 10, Prototype Slide # 9
JavaSever Without Prototype
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();
          }
     }

Doc 10, Prototype Slide # 10
Pre-Patterns way to Improve JavaServer
Use Inheritance

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

Doc 10, Prototype Slide # 11
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


Doc 10, Prototype Slide # 12
The Interface

interface ServerEngine
     {
     public ServerEngine clone( InputStream in, 
                                        OutputStream out );

     public void processClientRequest();
     }


Doc 10, Prototype Slide # 13
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();
               processClientRequest( cin, cout );
               }
          }

     private void processClientRequest( InputStream cin,
                                         OutputStream cout )
          {
          ServerEngine  handler;
          handler = serverPrototype.clone( cin, cout);
          handler.processClientRequest();
          }
     }
Doc 10, Prototype Slide # 14
Driver Program

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

Doc 10, Prototype Slide # 15

Consequences



Implementation Issues



----------