java设计模式--单例模式

来源:互联网 发布:1hhhhh域名访问升级 编辑:程序博客网 时间:2024/05/01 17:31

单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例。

单例模式特点:

1、单例类只能有一个实例。

2、单例类必须自己自己创建自己的唯一实例。

3、单例类必须给所有其他对象提供这一实例。

实例:

public class Singleton {    private static Singleton uniqueInstance = null;     private Singleton() {       // Exists only to defeat instantiation.    }     public static Singleton getInstance() {       if (uniqueInstance == null) {           uniqueInstance = new Singleton();       }       return uniqueInstance;    }    // Other methods...}


原创粉丝点击