浅谈Java常见设计模式
Posted 依荨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浅谈Java常见设计模式相关的知识,希望对你有一定的参考价值。
package com.lgli.behavior.delegatestrategy.v3;
import java.util.HashMap;
import java.util.Map;
/**
*
* 策略工厂
* @author lgli
*/
public class StrategyFactory {
private static final StrategyFactory strategyFactory
= new StrategyFactory();
private static final ControllerStrategy EMPTY_STRATEGY
= new EControllerStrategy();
private StrategyFactory(){}
private static final Map<String,ControllerStrategy> map
= new HashMap<>();
static {
map.put(StrategyKey.A.name(),
new AControllerStrategy());
map.put(StrategyKey.B.name(),
new BControllerStrategy());
map.put(StrategyKey.EMPTY.name(),
new EControllerStrategy());
}
public static StrategyFactory getInstance(){
return strategyFactory;
}
public ControllerStrategy getStrategy(String key){
ControllerStrategy controllerStrategy = map.get(key);
return controllerStrategy == null ?
EMPTY_STRATEGY : controllerStrategy;
}
enum StrategyKey{
A,B,EMPTY
}
}
package com.lgli.behavior.delegatestrategy.v3;
/**
* @author lgli
*/
public class DealController {
private static final DealController dealController
= new DealController();
private DealController(){}
public static DealController getInstance(){
return dealController;
}
public void deal(String key){
StrategyFactory strategyFactory = StrategyFactory
.getInstance();
ControllerStrategy strategy = strategyFactory
.getStrategy(key);
strategy.deal();
}
}
点个赞再走了,谢谢!
以上是关于浅谈Java常见设计模式的主要内容,如果未能解决你的问题,请参考以下文章