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);
        }
    
    }
    
  • 相关阅读:
    npm
    React
    php区分new static 和new self
    tiny java web server
    算法可视化
    在线markdown编辑器
    JAVA
    linux find命令
    自定义windows新建菜单
    floyd算法
  • 原文地址:https://www.cnblogs.com/fredkeke/p/9394252.html
Copyright © 2011-2022 走看看