zoukankan      html  css  js  c++  java
  • 单例设计模式-java

    /**
     * 饿汉式
     */
    class SingletonHungery{
        private static SingletonHungery s = new SingletonHungery();
        private SingletonHungery(){
    
        }
        public static SingletonHungery getInstance(){
            return s;
        }
    }
    
    /**
     * 懒汉式
     */
    
    class SingletonLazy{
        private static SingletonLazy s;
        private SingletonLazy(){
    
        }
        public static SingletonLazy getInstance(){
            if(s == null) {
                s = new SingletonLazy();
            }
            return s;
        }
    }
    
    public class SingletonDemo {
        public static void main(String[] args) {
            SingletonHungery s1 = SingletonHungery.getInstance();
            SingletonHungery ss1 = SingletonHungery.getInstance();
            System.out.println(s1 == ss1);
    
            SingletonLazy s2 = SingletonLazy.getInstance();
            SingletonLazy ss2 = SingletonLazy.getInstance();
            System.out.println(s2 == ss2);
        }
    
    }
    
  • 相关阅读:
    cg数据类型
    线程和流的历史遗留
    流的总结及小问题

    集合练习
    集合属性的整理
    集合
    整理
    面向对象中知识的薄弱点
    自己的小问题和数组常用的方法
  • 原文地址:https://www.cnblogs.com/fredkeke/p/9394252.html
Copyright © 2011-2022 走看看