Pages

Friday, 15 November 2013

Strategy Pattern ain't meant for Spring!

Right, so lets say you're writing a Spring MVC app and you decide, "I want to do seperate encapsulated algorithms that can be swapped to carry out a specific behaviour".

The classic response to this would be "you need the Strategy Pattern ma' boy!".  So, that's what I did, consider the code below...

Interface
public interface MealStrategy {
    cook(Meat meat);
}
First strategy
@Component
public class BurgerStrategy  implements
MealStrategy {
  @Autowired CookerDao cookeryDao;

  @Override
  public void cook(Meat meat) {
      cookeryDao.getBurger(meat);
  }
}
Next strategy...
@Component
public class SausageStrategy  implements
MealStrategy {
  @Autowired CookerDao cookeryDao;

  @Override
  public cook(Meat meat) {
      return cookeryDao.getSausage(meat);
  }
}
Context...
@Component
@Scope("prototype")
public class MealContext {
    private MealStrategy mealStrategy;

    public void setMealStrategy(MealStrategy strategy) {
        this.strategy = strategy;
    }

    public void cookMeal(Meat meat) {
        mealStrategy.cook;
    }
}
Now say this context was being accessed through an mvc controller, like...
@Autowired
private MealContext mealContext;

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Something makeMeal(Meat meat) {
    mealContext.setMealStrategy(new BurgerStrategy());
    mealContext.cookMeal(meat);
}

Normally, this would make perfect sense for use with the strategy pattern.  However, the one line that makes it all come crashing down is the line in the controller...

mealContext.setMealStrategy(new BurgerStrategy())

This will cause the new instance of the bean to be created outside Spring's application context.  This is curious, so how should you go about implementing a pattern within Spring if you can't use new to create a new instance?  After asking this question on StackOverflow, the conclusion I came to was to use the Dependency Injection pattern (as per the standard autowiring in Spring) and make individual injections of strategies.  Firstly, I'd need to amend the strategies so they have named components.

@Component("burger")
public class BurgerStrategy implements MealStrategy { ... }

@Component("sausage")
public class SausageStrategy implements SausageStrategy { ... }
Now in the controller, I have individual strategies to use, so rather than setting a strategy, I just pick the appropriate one that's injected.
@Resource(name = "burger")
MealStrategy burger;

@Resource(name = "sausage")
MealStrategy sausage;

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody Something makeMeal(Meat meat) {
    burger.cookMeal(meat);
}

No comments:

Post a Comment