David Gray

About David Gray

David is a software engineer with a passion for everything Java and the web. He is a server side java developer based in Derby, England by day and a Android, jQuery, jack of all trades by night.

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);
}

 

Reference: Strategy Pattern ain’t meant for Spring! from our JCG partner David Gray at the Code Mumble blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

4 Responses to "Strategy Pattern ain’t meant for Spring!"

  1. Taylor Mathewson says:

    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.

  2. George Andrianakis says:

    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

  3. Randy says:

    What’s Meat in your example? An interface or concrete class? Can you make burgers out of sausage meat? Bit of a lame example.

  4. ZakyAlvan says:

    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).

Leave a Reply


3 × = twenty four



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books