图解设计模式
Posted 手边星辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图解设计模式相关的知识,希望对你有一定的参考价值。
http://www.cnblogs.com/maowang1991/archive/2013/04/15/3023236.html
http://download.csdn.net/detail/zhangerqing/4835830
一、创建型:
(Factory)工厂:
(Builder)建造者:
public static <T extends BaseRQ> T creat(Class<T> cls) { try { T rq = cls.newInstance(); TmeUserAcc user = SecurityUserHolder.getCurrentUser(); rq.setAppBrh(user.getBrhId()); rq.setLoginName(user.getUserAccId()); rq.setAppNo(BaseRQ.PC); rq.setTimeStamp(DateUtil.getCurrDateTime()); return rq; } catch (Exception e) { log.error("创建RQ异常",e); } return null; }
(Singleton) 单例:http://www.cnblogs.com/shoubianxingchen/p/5748645.html
public class PropertiesUtil { private static class Inner { static{ prop = fillProperty("/global.properties"); } private static Properties prop; } private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class); public static Properties fillProperty(String propName) { InputStream in = null; try { in = PropertiesUtil.class.getResourceAsStream(propName); Properties prop = new Properties(); prop.load(in); return prop; } catch (IOException e) { e.printStackTrace(); } finally { if (null != in) try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } public static String getProperty(String key) { if (null != Inner.prop) { return Inner.prop.getProperty(key); } log.debug(" init prop failed."); return null; } public static String getProperty(String fileName, String key) { if (null != fileName && fileName.trim().length() != 0) { Properties prop = fillProperty(fileName); if (null != prop) { return prop.getProperty(key); } log.debug("can not find the file:" + fileName); } return null; } public synchronized static void fresh() { Inner.prop=PropertiesUtil.fillProperty("/global.properties"); } }
(Prototype)原型模式:(Copy)
public Object clone() throws CloneNotSupportedException { Prototype proto = (Prototype) super.clone(); return proto; } /* 深复制 */ public Object deepClone() throws IOException, ClassNotFoundException { /* 写入当前对象的二进制流 */ ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); /* 读出二进制流产生的新对象 */ ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return ois.readObject(); }
二、结构型:
(Adapter):适配器模式:类适配、实例适配、接口适配。
(Decorator、Bridge、Proxy):装饰器、桥接、代理
注意比较装饰器和代理:装饰可以选择被装饰的对象,而代理则不能。
(Facade、Composite、Flyweight):外观、组合、享元
三、行为型
(Strategy、Template Method)
(Observer、Iterator、Responsibility Chain、Command)观察者、迭代器、责任链、命令
(Visitor、Mediator、Interpreter):访问者、中介者、解释器
以上是关于图解设计模式的主要内容,如果未能解决你的问题,请参考以下文章
图解23种设计模式(TypeScript版)——前端必修内功心法