标签:运行 委托 name 生成 获取 color 修改 知乎 sha
整理自知乎;整理自知乎;整理自知乎
public interface FontProvider { Font getFont(String name); }
具体的实现类有三种,分别是FontProviderFromDisk,FontProviderFromNet,FontProviderFromSystem
public class FontProviderFromDisk implements FontProvider { @Override public Font getFont(String name) { System.out.printf("get %s from disk\n",name); return null; } } public class FontProviderFromNet implements FontProvider { @Override public Font getFont(String name) { System.out.printf("get %s from net\n",name); return null; } } public class FontProviderFromSystem implements FontProvider { @Override public Font getFont(String name) { System.out.printf("get %s from system\n",name); return null; } }
然后我们使用上面FontProvider的实现类,获取字体,但是我突然想增加个缓存的功能以提高效率!怎么办呢?第一种就是在各个FontProvider实现类中添加缓存的代码,这样显然不理想,万一我这有一百个FontProvider实现类呢,那岂不是得修改一百多个FontProvider的实现类?第二种就是利用静态代理控制方法的调用(从缓存中获取成功,则不调用委托类的getFont()方法,获取失败则老老实实调用委托类的getFont()方法),这样即使有一百个FontProvider实现类也可以保证就仅有一份实现字体缓存的代码。
public class CachedFontProvider implements FontProvider { private FontProvider fontProvider; //委托类 private Map<String, Font> cached; public CachedFontProvider(FontProvider fontProvider) { this.fontProvider = fontProvider; cached = new HashMap<>(); } /** * 绑定委托类,依靠这个方法可以运行时更换委托类 * @param fontProvider */ public void bindTo(FontProvider fontProvider) { this.fontProvider = fontProvider; cached.clear(); //委托类更换,缓存清除 } @Override public Font getFont(String name) { Font font = cached.get(name); if(font == null) { //从缓存中获取失败,调用委托类的getFont()方法 font = fontProvider.getFont(name); cached.put(name, font); return font; } else { //从缓存中获取成功,无需调用委托类的getFont()方法 return font; } } }
public class CachedColorProvider implements ColorProvider { private ColorProvider colorProvider; //委托类 private Map<String, Color> cached; public CachedColorProvider(ColorProvider colorProvider) { this.colorProvider = colorProvider; cached = new HashMap<>(); } /** * 绑定委托类,依靠这个方法可以运行时更换委托类 * @param ColorProvider */ public void bindTo(ColorProvider colorProvider) { this.colorProvider = colorProvider; cached.clear(); //委托类更换,缓存清除 } @Override public Color getColor(String name) { Color color = cached.get(name); if(Color == null) { //从缓存中获取失败,调用委托类的getColor()方法 Color = ColorProvider.getColor(name); cached.put(name, color); return color; } else { //从缓存中获取成功,无需调用委托类的getColor()方法 return color; } } } public class CachedShapeProvider implements ShapeProvider { private ShapeProvider shapeProvider; //委托类 private Map<String, Shape> cached; public CachedShapeProvider(ShapeProvider shapeProvider) { this.shapeProvider = shapeProvider; cached = new HashMap<>(); } /** * 绑定委托类,依靠这个方法可以运行时更换委托类 * @param shapeProvider */ public void bindTo(ShapeProvider shapeProvider) { this.shapeProvider = shapeProvider; cached.clear(); //委托类更换,缓存清除 } @Override public Shape getShape(String name) { Shape shape = cached.get(name); if(shape == null) { //从缓存中获取失败,调用委托类的getShape()方法 shape = shapeProvider.getShape(name); cached.put(name, shape); return shape; } else { //从缓存中获取成功,无需调用委托类的getShape()方法 return shape; } } }
public class CachedProviderHandler implements InvocationHandler { private Map<String, Object> cached = new HashMap<>(); private Object target; public CachedProviderHandler(Object target) { this.target = target; } public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Type[] types = method.getParameterTypes(); if (method.getName().matches("get.+") && (types.length == 1) && (types[0] == String.class)) { //控制getFont(),getColor(),getShape()等方法的访问 String key = (String) args[0]; Object value = cached.get(key); if (value == null) { value = method.invoke(target, args); cached.put(key, value); } return value; } return method.invoke(target, args); } } //生成FontProvider的代理对象 Proxy.newProxyInstance(FontProvider.class.getClassLoader(),new Class[]{FontProvider.class},new CachedProviderHandler(new FontProviderFromDisk())); //生成ShapeProvider的代理对象 Proxy.newProxyInstance(ShapeProvider.class.getClassLoader(),new Class[]{ShapeProvider.class},new CachedProviderHandler(new ShapeProviderFromDisk())); ...
标签:运行 委托 name 生成 获取 color 修改 知乎 sha
原文地址:http://www.cnblogs.com/fudashi/p/7117365.html