Creational Patterns «Prev Next»

ConcreteVehicleFactory Class - Exercise

Objective: Write creational classes for the course project.
In this step of the workflow, you will implement a concrete subclass of VehicleFactory named ConcreteVehicleFactory. Your job is to override the factory method createVehicle() so it returns a Vehicle instance chosen at runtime (based on the probability fields inherited from VehicleFactory).

Use the UML diagram below as the required class structure.
Class diagram for the ConcreteVehicleFactory
ConcreteVehicleFactory — Methods
+ ConcreteVehicleFactory()

+ ConcreteVehicleFactory(
    chanceCar: double,
    chanceBus: double,
    chanceBicycle: double,
    chancePedestrian: double
)

+ createVehicle() : Vehicle
Notes on notation
  • + indicates public visibility.
  • The first two entries are constructors (a no-args constructor and a parameterized constructor).
  • createVehicle() is the factory method and should return the abstract type Vehicle.
In the text area below, type or paste your solution. When you’re ready, click Submit.