Reputation: 2794
What would be the best way to implement a factory method using Spring?
@Override
public List<Prize> getPrizesForCustomer(final List<PizzaType> pizzaTypes)
{
List<Prize> prizeList = new ArrayList<>();
for (PizzaType type : pizzaTypes)
{
PizzaService prizePizzaService = PizzaFactory.getService(type);
prizeList = prizePizzaService.populatePrizeList();
}
}
return prizeList;
}
MyFactory
class PizzaFactory
{
public static PizzaService getService(final PizzaType pizza)
{
PizzaService pizzaService = null;
if (pizza.equals(PizzaType.CheesePizza))
{
pizzaService = new CheeseServiceImpl();
}
else if (pizza.equals(PizzaType.Veggie))
{
pizzaService = new VeggieServiceImpl();
}
// Possibly some more pizza styles here
return pizzaService;
}
}
Upvotes: 0
Views: 107
Reputation: 3080
You are making a confusion between factory method and spring application context.
What you should do is creating 2 beans in the application context one called cheeseService and the other one called veggieService. After that you should only create a static method getService that will return one of beans depending on the PizzaType from the spring application context.
Upvotes: 1