GofPatterns GofPatterns

Creational Patterns  «Prev  Next»

Vehicle Factory Consequences- Exercise

Vehicle Factory Class

Objective:Write an abstract class called VehicleFactory in place of direct instantiation with constructors.
In this exercise, you will continue building your course project by creating an abstract class called VehicleFactory which is shown in this diagram:

UML diagram

It is conventional to provide concrete subclasses that return particular kinds of vehicles, such as BusFactory, CarFactory, BicycleFactory, etc. However, we are going to need something just a little different. Instead, instances of this Factory will return instances of different vehicle subclasses with different frequencies. The exact percentages should be set in a constructor. However, there should also be a noargs constructor. For the defaults:
  1. Use an 80% probability of a Car with a 10% probability of either a Bus or a Bicycle.
  2. Enforce a constraint that the sum of the chances of all the different vehicle types is less than or equal to 1.0 (100%) and that no single chance is less than 0% (no negative probabilities)

Eventually, this Factory class will be used to create vehicles that appear at the intersection of our simulation. As additional kinds of vehicles are added later, only this class needs to change. Neither the preexisting vehicle classes nor the client classes that invoke createVehicle() need to change.
Note that this is an abstract class with an abstract createVehicle() method. This method will be filled in later in subclasses. In the text area below, type your answer.
Click the Submit button to submit the exercise.