首页 > 其他 > 详细

单例模式

时间:2018-01-30 22:19:59      阅读:207      评论:0      收藏:0      [点我收藏+]

单例模式的三个步骤
 1.构造方法私有化
 2.定义一个静态的本类成员
 3.设计一个静态的接口用来返回上面定义的那个类对象成员


package aaaa;

class singleton{
    
    public static singleton instance = null;    //静态的本类成员
    private singleton() {        //构造方法私有化
        
    }
    public static singleton getInstance() {        //设计一个静态的接口来返回上面定义的那类本类成员
        if(instance == null) {
            instance = new singleton();
        }
        return instance;
    }
    
    
}

public class demo1 {
    public static void main(String[] args) {
        singleton instance1 = singleton.getInstance();
        singleton instance2 = singleton.getInstance();
        
        System.out.println(instance1 + "  "+ instance2);
    }
}

 

单例模式

原文:https://www.cnblogs.com/1950137408lxj/p/8387273.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!