策略模式:Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. 定义一族算法类,将每个算法分别封装起来,让它们可以互相替换。策略模式可以使算法的变化独立于使用它们的客户端。
Copy public interface Strategy {
void algorithmInterface ();
}
public class ConcreteStrategyA implements Strategy {
@ Override
public void algorithmInterface () {
//具体的算法...
}
}
public class ConcreteStrategyB implements Strategy {
...
}
Copy public class StrategyFactory {
private static final Map < String , Strategy > strategies = new HashMap <>();
static {
strategies . put ( "A" , new ConcreteStrategyA() );
strategies . put ( "B" , new ConcreteStrategyB() );
}
public static Strategy getStrategy ( String type) {
if (type == null || type . isEmpty ()) {
throw new IllegalArgumentException( "type should not be empty." ) ;
}
return strategies . get (type);
}
}
Copy // 策略接口:EvictionStrategy
// 策略类:LruEvictionStrategy、FifoEvictionStrategy、LfuEvictionStrategy...
// 策略工厂:EvictionStrategyFactory
// 运行时动态确定,根据配置文件的配置决定使用哪种策略
public class Application {
public static void main ( String [] args) throws Exception {
EvictionStrategy evictionStrategy = null ;
Properties props = new Properties() ;
props . load ( new FileInputStream( "./config.properties" ) );
String type = props . getProperty ( "eviction_type" );
evictionStrategy = EvictionStrategyFactory . getEvictionStrategy (type);
UserCache userCache = new UserCache(evictionStrategy) ;
//...
}
}