设计模式-缓存工厂模式代码构造
public abstract class Product {
public abstract void doSomething();
public abstract void doSomething();
}
public class ConcreteProduct1 extends Product {
public void doSomething() {
//逻辑处理
}
}
public class ConcreteProduct2 extends Product {
public void doSomething() {
//逻辑处理
}
}
public class Factory {
private static final Map<String,Product> prMap = new HashMap();
public static synchronized Product createProduct(String type) throws Exception{
Product product =null;
//如果Map中已经有这个对象
if(prMap.containsKey(type)){
product = prMap.get(type);
}else{
if(type.equals("Product1")){
product = new ConcreteProduct1();
}else{
product = new ConcreteProduct2();
}
//同时把对象放到缓存容器中
prMap.put(type,product);
}
return product;
}
}
此模式是目前大多数应对高并发应对模式
地瓜园