I'm relatively new to Spring, and I climbed into the pit. I am trying to simulate cars. Each model has its own builder object, and I have a BuilderFactory that returns the correct builder based on the user's choice from the web application.
So, Iβm looking for suggestions on how to approach this problem when I need to create several separate cars, but I donβt know what type of vehicle I will need before the lead time, and each vehicle should be unique to the user.
What I have at the moment is shown below. The problem that I have at the moment is that, since individual builders are single, they are individual vehicles. I need them to be prototypes. I know that everything looks pretty terrible, so I'm sure there should be a better way to do this.
The top level from the web application is as follows:
Vehicle vehicle = vehicleBuilderFactory.getBuilder(platform).build();
My vehicleBuilderFactory looks like this:
@Service public class VehicleBuilderFactory { @Autowired Discovery3Builder discovery3Builder; @Autowired Discovery4Builder discovery4Builder; // Lots of @Autowired statements here. @Autowired FreeLander2010Builder freeLander2010Builder; public VehicleBuilder getBuilder(Platform platform) { switch (platform.getId()) { case 1: return discovery3Builder; case 2: return discovery4Builder; // Lots of case statements here case 44: return freeLander2010Builder; default: return null; } } }
which itself looks pretty awful. Each individual builder is as follows:
@Service public class DefenderBuilder implements VehicleBuilder { @Autowired Defender defender;
and finally an individual car
@Service @Scope("prototype") public class Defender extends Vehicle { }
The main problem now is that since the builders are single, so are the vehicles, and I need them to be prototypes because User A Defender is different from User B Defender.