Refactor the course-project vehicle model so repeated immutable vehicle descriptions are canonical flyweights while each simulated vehicle occurrence retains independent extrinsic state. The finished design must demonstrate sharing through a factory without turning the domain model into a global cache.
Assume a large simulation creates hundreds of thousands of vehicle events but uses a small catalog of vehicle types. Stable values such as category, dimensions, and normal velocity are candidates for intrinsic state. Event ID, direction, lane, arrival tick, current speed, and queue position are extrinsic and must not be stored in a shared flyweight.
The original exercise suggested subclassing a factory and assumed constant velocity. Your solution should make the state boundary explicit instead. Factory inheritance is optional. Composition is preferable when the only requirement is to substitute canonical creation behind an existing interface.
VehicleType
FlyweightVehicleFactory
VehicleEvent
Submit the flyweight class, canonical key, factory, event or context type, and tests. Include a brief state table identifying each field as intrinsic or extrinsic. Explain factory ownership, equality, thread-safety assumptions, and why the chosen key cannot merge semantically different vehicle descriptions. Your response should also state the workload assumption that motivates the optimization.
The solution is evaluated for a correct intrinsic/extrinsic split, immutable shared state, value-based canonicalization, bounded and explicit ownership, independent event identity, and deterministic tests. Merely caching mutable vehicle objects, comparing only reference identity, or placing direction and queue position inside the flyweight fails the pattern's correctness criteria.
Type your answer in the text area below and click Submit. Include both implementation and verification evidence in your response.