美文网首页
常用设计模式

常用设计模式

作者: 浩仔_Boy | 来源:发表于2019-03-12 15:52 被阅读0次

    1.单例模式:

    class SimpleInstance {
        //volatile变量仍然有工作内存的拷贝,但是由于它特殊的操作顺序性规定,所以看起来如同直接在主内存中读写访问一般
        private static volatile SimpleInstance instance = null;
    
        public static SimpleInstance getInstance() {
            if (instance == null) {
                synchronized (SimpleInstance.class) {
                    if (instance == null) {
                        instance = new SimpleInstance();
                    }
                }
            }
            return instance;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:常用设计模式

          本文链接:https://www.haomeiwen.com/subject/ozrnpqtx.html