How to create prototype objects from a singleton? (Design assistance required)

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; // Loads of Defender specific setters ommitted @Override public Vehicle build() { return 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.

+6
source share
2 answers

You can use Spring ObjectFactory to serve a prototype with limited visibility of beans from a single line bean range. Usage is quite simple:

 @Component class DefenderBuilder implement VechicleBuilder { @Autowired ObjectFactory<Defender> defenderFactory; Defender build() { return defenderFactory.getObject() } } @Component @Scope("prototype") class Defender { } 

This returns a new Defender each time defenderFactory.getObject () is called.

+9
source

Without reading too many details, you say that you want to create Prototype beans from a singleton, possibly using a search in the IoC container.

Section 3.4.6.1 Entering the Spring Search Method The documentation describes how to do this without losing control inversion, that is, without your beans, knowing the bean repository.

I used ServiceLocatorFactoryBean to solve a similar problem earlier. The Javadoc class level is superb and provides some clear examples.

0
source

Source: https://habr.com/ru/post/889090/


All Articles