GofPatterns GofPatterns

Structural Patterns  «Prev  Next»

Flyweight Pattern - Exercise

The Flyweight pattern

Objective: Convert vehicles to Flyweights.
There are not really likely to be enough vehicles and pedestrians in our simulation to motivate the use of the Flyweight pattern.
For the purposes of this project, we need to assume that the various Vehicle classes are immutable (that velocity is constant). A Factory Method is already used to create the instances. So it is quite easy to make the vehicles flyweights merely by subclassing VehicleFactory with a FlyweightVehicleFactory.
More changes to the Vehicle class are required. Therefore, we will be doing this exercise mostly for the sake of practice .
The FlyweightVehicleFactory class has exactly the same public interface as the VehicleFactory class you have seen before. Only the private implementation is different. I will leave it up to you whether or not to use this Flyweight version of the VehicleFactory or the concrete one you developed in the last module.
Type your answer in the text area below and then click the Submit button to see the result.