Hacker News new | ask | show | jobs
by astral303 4644 days ago
Hmm... how about...

@Service public class SandwichFactoryImpl implements SandwichFactory {

	    @Autowired
	    public EmptySandwichFactory emptySandwichFactory;

	    @Autowired
	    public FoodComponentFactoryFactory foodComponentFactoryFactory;

	    @Override
	    public Sandwich makeSandwich(Collection<FoodComponentDescription> foodComponentDescriptions) {

	        Sandwich sandwich = emptySandwichFactory.newSandwich();

	        for (FoodComponentDescription foodComponentDescription : foodComponentDescriptions) {
	            FoodComponentFactory foodFactory = foodComponentFactoryFactory.getFactory(foodComponentDescription.getType());

	            for (int i = 0; i < foodComponentDescription.getQuantity(); i++) {
	            	sandwich.addFood(foodFactory.create());
	            }
	        }
	        
	        return sandwich;
	    }

	    public SandwichDTO makeMyFavoriteSandwich() {
	        Collection<FoodComponentDescription> components = Lists.newArrayList();

	        components.add(new FoodComponentDescription(FoodType.BREAD, 1));
	        components.add(new FoodComponentDescription(FoodType.BACON, 2));
	        components.add(new FoodComponentDescription(FoodType.BREAD, 1));

	        return makeSandwich(components);
	    }

	}
1 comments

This is much more inspired. I sort of feel sympathetic, because of the potential mental anguish you were likely to have incurred while writing it.