zoukankan      html  css  js  c++  java
  • 图解设计模式

    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");
        }
    
    }
    View Code

      (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):访问者、中介者、解释器

  • 相关阅读:
    java对象的实例化过程
    关键字super
    方法的重写
    继承
    JavaBean
    this关键字
    类的构造方法
    四种访问权限修饰符
    封装和隐藏
    初入博客园——你我共勉,至我的准读者朋友们
  • 原文地址:https://www.cnblogs.com/shoubianxingchen/p/6699633.html
Copyright © 2011-2022 走看看