1:// ALL SOURCES FOR PROTOTYPE DESIGN PATTERN
   2:
   3:/**
   4: * Code for Prototype Design Pattern by Futrelle, 7/21/03.
   5: * Main point is that Route is the prototype used to create
   6: * a Trip.  The Trip retains the Route as its timetable but
   7: * can change the duration in the legs in its tripLegs copy
   8: * of the Route.
   9: */
  10:
  11:public class Test {
  12:    
  13:    public static void main(String[] a) {
  14:        Route r = new Route();
  15:        r.addLeg(20,"Boston","Hanover",30);
  16:        r.addLeg(25,"Hanover","Plymouth",35);
  17:        Trip tr = new Trip(r);  // Prototype Design Pattern
  18:        System.out.println("Route is\n" + r.legs); // Entire route
  19:        System.out.println("Timetable is\n" + tr.tripLegs); // Timetable
  20:        tr.setDuration(1,45,tr.tripLegs);
  21:        System.out.println("Route unchanged, timetable changed\n"); 
  22:        System.out.println("Route is\n" + r.legs); // Entire route
  23:        System.out.println("Timetable is\n" + tr.tripLegs); // Timetable    
  24:    } // main()
  25:} 
  26:
  27:/* Output of the above is:
  28:Route is
  29:[Leg: Length is 20, starts at Boston, ends at Hanover, with duration 30
  30:, Leg: Length is 25, starts at Hanover, ends at Plymouth, with duration 35
  31:]
  32:Timetable is
  33:[Leg: Length is 20, starts at Boston, ends at Hanover, with duration 30
  34:, Leg: Length is 25, starts at Hanover, ends at Plymouth, with duration 35
  35:]
  36:Route unchanged, timetable changed
  37:
  38:Route is
  39:[Leg: Length is 20, starts at Boston, ends at Hanover, with duration 30
  40:, Leg: Length is 25, starts at Hanover, ends at Plymouth, with duration 35
  41:]
  42:Timetable is
  43:[Leg: Length is 20, starts at Boston, ends at Hanover, with duration 30
  44:, Leg: Length is 25, starts at Hanover, ends at Plymouth, with duration 45
  45:]
  46:*/
  47:/**
  48: * Code for Prototype Design Pattern by Futrelle, 7/21/03.
  49: * Main point is that Route is the prototype used to create
  50: * a Trip.  The Trip retains the Route as its timetable but
  51: * can change the duration in the legs in its tripLegs copy
  52: * of the Route.
  53: */
  54:public class Leg {
  55:    int len;
  56:    String start;
  57:    String end;
  58:    int duration;
  59:    
  60:    public Leg(int _len, String _start, String _end, int _duration) {
  61:        len = _len;
  62:        start = _start;
  63:        end = _end;
  64:        duration  = _duration;
  65:    }
  66:
  67:    public Leg(Leg originalLeg) {
  68:        len = originalLeg.len;
  69:        start = originalLeg.start;
  70:        end = originalLeg.end;
  71:        duration  = originalLeg.duration;
  72:    }
  73:    
  74:    void setDuration(int _duration) {
  75:        duration  = _duration;
  76:    }
  77:    
  78:    public String toString() {
  79:        return "Leg: Length is " + len +
  80:        ", starts at " + start +
  81:        ", ends at " + end +
  82:        ", with duration " + duration + "\n";
  83:    }
  84:}
  85:import java.util.*;
  86:
  87:/**
  88: * Code for Prototype Design Pattern by Futrelle, 7/21/03.
  89: * Main point is that Route is the prototype used to create
  90: * a Trip.  The Trip retains the Route as its timetable but
  91: * can change the duration in the legs in its tripLegs copy
  92: * of the Route.
  93: */
  94:
  95:public abstract class  AbstractRoute  {
  96:    
  97:    Vector legs;
  98:    
  99:    AbstractRoute(){
 100:        legs = new Vector();
 101:    }
 102:    void addLeg(int len, String start, String end, int dur) {
 103:        legs.add(new Leg(len,start,end, dur));
 104:    }
 105:    
 106:    void setDuration(int legNum, int _duration, Vector legs) {
 107:        ((Leg)(legs.get(legNum))).duration = _duration;
 108:    }
 109:    
 110:    // clone() omitted after studying Joshua Bloch's Effective Java
 111:    
 112:} // class Route
 113:
 114:import java.util.*;
 115:/**
 116: * Code for Prototype Design Pattern by Futrelle, 7/21/03.
 117: * Main point is that Route is the prototype used to create
 118: * a Trip.  The Trip retains the Route as its timetable but
 119: * can change the duration in the legs in its tripLegs copy
 120: * of the Route.
 121: */
 122:
 123:public  class  Route extends AbstractRoute {
 124:    
 125:    public Route() {
 126:        super();
 127:    } 
 128:    
 129:    
 130:} // class Route
 131:
 132:import java.util.*;
 133:
 134:/**
 135: * Code for Prototype Design Pattern by Futrelle, 7/21/03.
 136: * Main point is that Route is the prototype used to create
 137: * a Trip.  The Trip retains the Route as its timetable but
 138: * can change the duration in the legs in its tripLegs copy
 139: * of the Route.
 140: */
 141: 
 142:public  class  Trip extends AbstractRoute  {
 143:    
 144:    Route timetable;
 145:    Vector tripLegs;
 146:/**
 147: *  Prototype Design Pattern here - Trip has the same schedule as Route.
 148: * Deep copy of Leg objects in tripLegs.
 149: */
 150:    public Trip(Route myRoute) {
 151:        timetable = myRoute;
 152:        tripLegs = new Vector();
 153:        Iterator it = myRoute.legs.iterator();
 154:        while(it.hasNext()) {
 155:            tripLegs.add(new Leg((Leg)it.next()));
 156:        }
 157:    }
 158:    
 159:} // class Trip
 160:
 161: