COM1204 Prototype Design Pattern Example by Prof. Futrelle, 7/21/2003


Design (should be done first)

There's a bus system that has buses and routes. Each bus has a driver and takes a trip, trying to follow a route. The route has stops and for each stop, the leg to the next stop. (The leg after the last is null.) But the focus of this little example is the differentiation between a Route and a Trip. A Route is a description of bus route with fixed, prescheduled times. A Trip represents the actual times that were required to traverse each leg.

The classes

At this point you should probably begin to look at the page containing all the sources. It is a file that is color-coded with all lines numbered. This page is here. Below this list of classes is some discussion of the code tied to the various lines. You might want to open the sources page in separate browser window in order to see the discussion and the source in parallel.

Test.java
Creates a Route and a Trip. Then shows that changing a leg of the trip does not affect the corresponding leg in the Route.
Leg.java
Defines a leg of a Route or Trip with a length, e.g., in miles, a start and end string, e.g., cities and a duration, e.g., in minutes.
AbstractRoute.java
The parent class of both Route and Trip. Allows a Leg to be added and allows the duration of a chosen Leg to be altered (needed for updating a Trip Leg to the real, not scheduled, value).
Route.java
Essentially a concrete class that Extends AbstractRoute but otherwise adds nothing.
Trip.java
This is where the important step in the Prototype Design Pattern is taken. Trip has a reference to its corresponding Route as well as its own Vector of Leg objects that it can vary. (It is not supposed to alter Route.) The constructor Trip(Route) creates new Legs from the Legs in Route so that changes in them do not affect the original Legs. The creation of a Trip from an existing, filled-in, Route is the essence of this Pattern. The Route is the Prototype that is used to create the Trip.

Some discussion of the code (referencing line numbers in the source code)

Line 17 (Test)
The duration of the second leg in the tripLegs of tr (Trip) is changed from 35 to 45. The output in line 40 shows that the value is unchanged in r (Route) but line 44 shows the change in tripLegs in tr (Trip).
Line 67 (Leg)
This is an important point: A new Leg is created using a previous one from which the four field values are copied. So this is what lies at the base of the Prototype Pattern in our case -- creation of a new object, a Leg, based on a Prototype, another Leg object. This constructor is used at line 155 during the construction of a Trip from a Route Prototype.
Line 78 (Leg)
Each Leg has a toString() method that prints out its four fields in a very readable way. When a Vector of Leg objects is printed out as in Lines 22 and 23, it prints in the form [<object1>,<object2>,<object3>, ....]. But since each object in the Vector is a Leg with a toString() method, the output is quite readable. (See the square brackets in Lines 29 and 31 which enclosed the two comma-separated Leg descriptions.)
Line 99 (AbstractRoute)
This abstract class has a constructor but it can not be called directly because no instance of an abstract class can be created. Instead, it is called at Line 126 to construct a Route, a class that extends AbstractRoute. The notation super() at Line 126 is a call to the constructor of the superclass of Route, which is AbstractRoute.
Line 144 (Trip)
Trip maintains its own copy of its Route in timetable. But it has an additional Vector of Legs in tripLegs, Line 145.

Return to COM1204 homepage.