COMP 3670 - Understanding Objects

Alex Bunardzic, British Columbia Institute Of Technology (BCIT)

 VRS Code (Week 10):

Click here to download the zipped source file for the VRS

The Abstractions

We have decided to introduce four main abstractions -- Vehicle, WorkOrder, Worker and Manager:

public interface Vehicle{
    public void move();
}

interface WorkOrder {
    public void addToWorkOrder(Object workItem);
    public int getWorkOrderSize();
    public Object getNextWorkItem();
}

interface Worker {
    public void work(WorkOrder workOrderIn);
    public void setName(String nameIn);
    public String getName();
}

interface Manager {
    public void commenceWork(String[] listOfItemsIn);
}



 

The Implementation

VehicleWorkOrder:

class VehicleWorkOrder implements WorkOrder{
    private java.util.Vector vehicles;
    private int pointer;

    public VehicleWorkOrder(){
        vehicles = new java.util.Vector();
        pointer = -1;
    }

    public void addToWorkOrder(Object workItem){
        vehicles.add(workItem);
    }

    public int getWorkOrderSize(){
        return vehicles.size();
    }

    public Object getNextWorkItem(){
        pointer++;
        return vehicles.elementAt(pointer);
    }
}
 

VehicleManager:

class VehicleManager implements Manager{

    public void commenceWork(String[] listOfItemsIn){
        Worker worker = new VehicleWorker("Jim");
        worker.work(prepareWorkOrder(listOfItemsIn));
    }

    private WorkOrder prepareWorkOrder(String[] listOfItems){
        WorkOrder wo = new VehicleWorkOrder();
        for(int i = 0; i < listOfItems.length; i++){
            try{
                wo.addToWorkOrder((Vehicle)Class.forName(listOfItems[i]).newInstance());
            }
            catch(Exception e){
                System.out.println("Error in list " + e);
            }
        }
        return wo;
    }
}
 
 

VehicleWorker:


class VehicleWorker implements Worker{
    private String name;

    public VehicleWorker(String nameIn){
        name = nameIn;
    }

    public void work(WorkOrder workOrderIn){
        WorkOrder workOrder = workOrderIn;
        Vehicle vehicle;
        for(int i = 0; i < workOrder.getWorkOrderSize(); i++){
            try{
                vehicle = (Vehicle)workOrder.getNextWorkItem();
                vehicle.move();
            }
            catch(Exception e){
            }
        }
    }

    public void setName(String nameIn){
        name = nameIn;
    }

    public String getName(){
        return name;
    }
}
 

Finally, any kind of vehicle implementation (say, a Bus):

class Bus implements Vehicle {
    public void move(){
        System.out.println("Bus is moving...");
    }
}

The application (VRS) will initiate the system in the following manner:

class VRS {
    public static void main(String[] args) {
        Manager manager = new VehicleManager();
        manager.commenceWork(args);
    }
}

In order to run the application, we should pass the list of vehicles to it from the command line, as follows:

java VRS Bus Submarine Helicopter Bike Truck CruiseShip

The list of the vehicles is variable, and is up to the user.

Note: instead of passing the list of the vehicles from the command line, we could use Java properties file to maintain the list, or some other persistence mechanism (i.e. RDBMS, flat files, mail server and so on).


Discussion

VRS system is characterized by extreme simplicity. It does not contain any explicit logic (no if-then-else or switch-case statements), and yet it knows how to make appropriate vehicles behave appropriately. This is due to the careful planning and the division of labor between objects.

The Manager prepares the Work Order from the list of items that was handed over to him. He also decides on which worker will carry on the workload. Then he sends a message to the worker to perform the work, by handing the Work Order to the worker.

The worker simply reads the Work Order and for each work item sends a message to the Vehicle to move.

This system makes it easy to introduce new, never before defined vehicles. All we have to do is define the new vehicle, compile it, and then submit it to the Manager, who will include it in the Work Order. The worker will know how to cope with a completely new vehicle.