Java 单例模式

关键字: singleton 


单例模式有以下的特点:
1.单例类只可有一个实例。
2.单例类必须自己创建自己这惟一的实例。
3.单例类必须给所有其他对象提供这一实例。


Java代码 
package com.xyq.demo;   
  
public class Singleton {   
    private static Singleton singleton;   
    private Singleton() {   
    };   
    public static Singleton getInstance() {   
        if (singleton == null)   
            singleton = new Singleton();   
        return singleton;   
    }   
}   
  
// 测试类   
class singletonTest {   
    public static void main(String[] args) {   
        Singleton s1 = Singleton.getInstance();   
        Singleton s2 = Singleton.getInstance();   
        if (s1 == s2)   
            System.out.println("s1 is the same instance with s2");   
        else  
            System.out.println("s1 is not the same instance with s2");   
    }   
}  


package com.xyq.demo;


public class Singleton {
private static Singleton singleton;
private Singleton() {
};
public static Singleton getInstance() {
if (singleton == null)
singleton = new Singleton();
return singleton;
}
}


// 测试类
class singletonTest {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1 == s2)
System.out.println("s1 is the same instance with s2");
else
System.out.println("s1 is not the same instance with s2");
}
} 输出:  s1 is the same instance with s2


 


恶汉式单例类


Java代码 
package com.xyq.demo;   
  
public class Singleton {   
    private static final Singleton instance = new Singleton();   
    private Singleton() {   
    }   
    public static Singleton getInstance() {   
        return instance;   
    }   
}   
  
// 测试类   
class singletonTest {   
    public static void main(String[] args) {   
        Singleton s1 = Singleton.getInstance();   
        Singleton s2 = Singleton.getInstance();   
        if (s1 == s2)   
            System.out.println("s1 is the same instance with s2");   
        else  
            System.out.println("s1 is not the same instance with s2");   
    }   
}  


package com.xyq.demo;


public class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
}
public static Singleton getInstance() {
return instance;
}
}


// 测试类
class singletonTest {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1 == s2)
System.out.println("s1 is the same instance with s2");
else
System.out.println("s1 is not the same instance with s2");
}
}
 输出:  s1 is the same instance with s2


 


懒汉式单例类


Java代码 
package com.xyq.demo;   
  
public class Singleton {   
    private static Singleton instance = null;   
  
    /**  
     * 私有的默认构造子,保证外界无法直接实例化  
     */  
    private Singleton() {   
    }   
  
    /**  
     * 静态工厂方法,返还此类的惟一实例  
     */  
    synchronized public static Singleton getInstance() {   
        if (instance == null) {   
            instance = new Singleton();   
        }   
        return instance;   
    }   
}   
  
// 测试类   
class singletonTest {   
    public static void main(String[] args) {   
        Singleton s1 = Singleton.getInstance();   
        Singleton s2 = Singleton.getInstance();   
        if (s1 == s2)   
            System.out.println("s1 is the same instance with s2");   
        else  
            System.out.println("s1 is not the same instance with s2");   
    }   
}  


package com.xyq.demo;


public class Singleton {
private static Singleton instance = null;


/**
* 私有的默认构造子,保证外界无法直接实例化
*/
private Singleton() {
}


/**
* 静态工厂方法,返还此类的惟一实例
*/
synchronized public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}


// 测试类
class singletonTest {
public static void main(String[] args) {
Singleton s1 = Singleton.getInstance();
Singleton s2 = Singleton.getInstance();
if (s1 == s2)
System.out.println("s1 is the same instance with s2");
else
System.out.println("s1 is not the same instance with s2");
}
}
 输出:  s1 is the same instance with s2


我写的这些仅适用于公司面试,如果要想好好学习设计模式,得全面的看看这方面的资料


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值