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); }
I can’t believe I’m about to say this, but this is one part where Tapestry does well. Their Strategy Builder is very nice, though it’s assumes your Strategy Selector is based on the class of the argument. It requires a minimal amount of code. http://tapestry.apache.org/strategybuilder-service.html I’d love to see that get ported to Spring.
A way to dynamically retrieve Spring beans (in your case the burger and sauce strategies) without injecting all the dependencies is to use the ServiceLocatorFactoryBean from Spring along with a custom interface that returns whose return type is your Strategy interface
What’s Meat in your example? An interface or concrete class? Can you make burgers out of sausage meat? Bit of a lame example.
Why not using spring’s @Configurable on every strategy class and register each of them in on xml context-config with prototype scope? So you can use ‘new’ to create strategy object but still managed in spring container (autowired property injected behind the screen etc).
Hi, i’m actually new to spring, can you provide a complete example of doing this ? by using annotation approach.
Please look on this github repo https://github.com/zakyalvan/spring-configurable-sample
It’s perfect ! Very appreciate for the help.
Please look at this repo https://github.com/zakyalvan/spring-configurable-sample
Hi,
My @Autowire is not working ( null ) after using the @Configurable, do you have any idea ?
I have aspectj working in my Audit trail module which i believe should’t be the spring-tx and spring-context-support. I’m not using @EnableSpringConfigured but i have in my applicationContext.xml, is this matter ?
my code :
http://www.snipplr.com/view/98501/strategic-pattern-v2/
Hi,
My @Autowire actionBulletinService in abstract class Dashboard is not working ( null ) after using the @Configurable, do you have any idea ?
I have aspectj working in my Audit trail module which i believe should’t be the spring-tx and spring-context-support problem. I’m not using @EnableSpringConfigured but i have context:spring-configured in my applicationContext.xml, is this matter ?
my code :
http://www.snipplr.com/view/98501/strategic-pattern-v2/
please ignore my previous code
new code :
http://www.snipplr.com/view/98502/strategic-pattern-v3/
I think is the pom configuration problem ( aspectj-maven-plugin )
do you have any idea ?
my pom.xml :
http://www.snipplr.com/view/98577/pom/
Just update my repository (https://github.com/zakyalvan/spring-configurable-sample), i have changed samples use case to fulfill your need. Sorry, I didn’t understand your samples, aaaand I’m so tired today :)).
seem nothing wrong with your maven’s plugin configuration.
The Strategy pattern works fine with Spring (as does any other pattern). But there is a time when to use it and when not to use it. With Springs very lenient use of the IoC principle, and that spring managed beans default to singletons, generally we get around the use of a strategy, but if prototypes are necessary for your design (this is often the case when converting from an older framework to spring) then the strategy pattern has its intended use.
Simple declare your beans with a scope = “prototype”.
Let’s say with time strategies become 100 ..won’t the controller class be spoiled with too many dependencies of each strategy using @Resource?