0

This is the scenario in which I have to apply some creational pattern.

Build a fleet of 10 cars with the following features. Five cars should have all the features and five should be built without the lidar and ultrasonic sensors:

  1. Radar sensors dotted around the car monitor the position of vehicles nearby.
  2. Video cameras detect traffic lights, read road signs and keep track of other vehicles, while also looking out for pedestrians and other obstacles.
  3. Lidar sensors help to detect the edges of roads and identify lane markings by bouncing pulses of light off the car’s surroundings.
  4. Ultrasonic sensors in the wheels can detect the position of curbs and other vehicles when parking.
  5. Finally, a central computer analyses all of the data from the various sensors to manipulate the steering, acceleration and braking.

My initial guess is that it should follow abstract factory method. Following is the initial design but I am not sure whether this is correct or not.

Car factory interface
carCreatefactory implements Car factory
Products:
Sensors
Radar sensor implements Sensors
Lidar Sensor implements Sensors
Ultrasonic sensor implements Sensor

Camera
Video Camera implements Camera

Computer
Central Computer implements Computer

Client:
Fleet

I am confused about the central computer since it doesn't really seem to fit. Secondly, in this design a car can have only one sensor and not all. Would this pattern fit by changing some logic? Or a different patter should be used?

Christophe
  • 81,699

1 Answers1

2

Candidate creational patterns

Build a fleet of 10 cars with the following features. Five cars should have all the features and five should be built without the lidar and ultrasonic sensors

Singleton is of course excluded.

Prototype is not really a candidate, because it would require that you have already 1 car with all the features and one car without the radar, in order to clone them.

Factory method could be considered, if the car creation interface is always the same. But can it really be with all these features ? And what if I have a new car model with some more features ?

Abstract factory could be a candidate. Car models could be different families of abstract products. Car with and without radar could be different products, or could be the same product created with different parameters in the constructor.

Builder could be a candidate. A car would be a complex objet with a construction process that could be abstracted from the concrete car representation.

And the winner is... ?

Let's read again and take care of the details:

Build a fleet of 10 cars with the following features. Five cars should have all the features and five should be built without the lidar and ultrasonic sensors.

Would this be all, I'd have opted for the abstract factory as you, each feature being a boolean parameter in the factory call.

But your text gives you the detail of the features and their dependency:

  1. Radar sensors ... around the car ...
  2. Video cameras ...
  3. Lidar sensors ... pulses of light ...
  4. Ultrasonic sensors in the wheels ...
  5. ... a central computer ... data from the various sensors ... manipulate the steering, acceleration and braking.

This suggest that you have to make sure that the parts in your Car know each other according to these requirements. Eg the computer object of a specific car object knows each of the sensor objects of that car so that it can send them queries.

Taking this into consideration, it appears that the winner is the builder pattern. It's the only pattern designed to cope with a complex construction process and assembly of parts.

Christophe
  • 81,699