2013-07-13 26 views
12

aşağıdaki stratejileri var, bahar kullanıyorum Say ...Strateji desen

Arayüz

public interface MealStrategy { 
    cook(Meat meat); 
} 

İlk strateji

@Component 
public class BurgerStrategy implements 
MealStrategy { 
    @Autowired CookerDao cookeryDao; 

    @Override 
    public void cook(Meat meat) { 
     cookeryDao.getBurger(meat); 
    } 
} 

Sonraki strateji ...

@Component 
public class SausageStrategy implements 
MealStrategy { 
    @Autowired CookerDao cookeryDao; 

    @Override 
    public cook(Meat meat) { 
     return cookeryDao.getSausage(meat); 
    } 
} 

Cont ext ...

@Component 
@Scope("prototype") 
public class MealContext { 
    private MealStrategy mealStrategy; 

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

    public void cookMeal(Meat meat) { 
     mealStrategy.cook; 
    } 
} 

Şimdi
@Autowired 
private MealContext mealContext; 

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

bağlam bir bileşen olmalı ... Bu bağlamda bir mvc denetleyicisi gibi aracılığıyla erişilen edildiğini söylüyor? Ne zaman ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı ı Fasulyelerin hepsinin yukarıdaki gibi bileşenler olması mı gerekiyor yoksa ek açıklamalarım yanlış mı?

En büyük sorgum gerçekten bir Spring MVC uygulamasında böyle bir bağlam kullanabilir misiniz? Ben de @Scope (prototype) kullanarak sahip olduğum problem, Dao'nun enjekte edilmemesi gibi stratejilerdeki sıfırdan bir işaretçi döndürmesi anlamına geliyor.

Yukarıdaki deseni yayı kullanarak nasıl uygularım ve ayrıca diş ipi güvenli midir? Denediğim şey bile mümkün mü?

+0

Tam olarak sorunun nedir? Bütün bunlar benim için çalışıyor – morgano

+0

Ben – david99world

+0

yaptım gibi bir bağlam olması için iş parçacığı güvenli olup olmadığını bilmek istiyorum, çünkü ben de 'yeni' yaptım çünkü bu bahar bağlam dışında çalışır? – david99world

cevap

16

Basit Bağımlılık Enjeksiyonu kullanırdım.

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

@Component("sausage") 
public class SausageStrategy implements MealStrategy { ... } 

Kontrolör

Seçenek A:

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

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

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

Seçenek B:

@Autowired 
BeanFactory bf; 

@RequestMapping(method = RequestMethod.POST) 
public @ResponseBody Something makeMeal(Meat meat) { 
    bf.getBean("burger", MealStrategy.class).cookMeal(meat); 
} 

Sen sırasında derleme yazım hatalarını yakalamak için yerine metinsel isimlerinden JSR-330 eleme oluşturmayı seçebilirsiniz zaman.

Ayrıca bkz: somut strateji çok sık sağlanan parametrelere kadar dayanan çalışma zamanında belirlenir yana aşağıdaki gibi

How to efficiently implement a strategy pattern with spring?

@Resource vs @Autowired

20

, bir şey önermek istiyorum.

@Component 
public class BurgerStrategy implements MealStrategy { ... } 

@Component 
public class SausageStrategy implements MealStrategy { ... } 

Ardından verilen denetleyicisi (a anahtar olarak fasulye adıyla) harita haline tür tüm stratejilerini enjekte ve istek üzerine ilgili strateji seçin.

@Autowired 
Map<String, MealStrategy> mealStrategies = new HashMap<>; 

@RequestMapping(method=RequestMethod.POST) 
public @ResponseBody Something makeMeal(@RequestParam(value="mealStrategyId") String mealStrategyId, Meat meat) { 
    mealStrategies.get(mealStrategyId).cook(meat); 

    ... 
} 
+1

Bu cevap, strateji modelinin daha doğru bir şekilde uygulanmasını sağlar – Cuga