参考:http://www.runoob.com/design-pattern/singleton-pattern.html
源码下载:https://github.com/gpqhl0071/pattern.git
1. 对象在整个工程的运行周期内,值创建一个对象
代码
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
public class SingleObject {
// 创建 SingleObject 的一个对象
private static SingleObject instance = new SingleObject();
// 让构造函数为 private,这样该类就不会被实例化
private SingleObject() {
}
// 获取唯一可用的对象
public static SingleObject getInstance() {
return instance;
}
public void showMessage() {
System.out.println("Hello World!");
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single.test;
import com.pattern.single.SingleObject;
public class SingletonPatternDemo {
public static void main(String[] args) {
// 不合法的构造函数
// 编译时错误:构造函数 SingleObject() 是不可见的
// SingleObject object = new SingleObject();
// 获取唯一可用的对象
SingleObject object = SingleObject.getInstance();
// 显示消息
object.showMessage();
}
}
结果:
Hello World!
6中单例的实现,代码如下:
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 懒汉式,线程不安全
* @author: gao peng
* @date: 2016年3月3日 上午11:33:16
*
*/
public class Singleton1 {
private static Singleton1 instance;
private Singleton1() {
}
public static Singleton1 getInstance() {
if (instance == null) {
instance = new Singleton1();
}
return instance;
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 懒汉式,线程安全
* @author: gao peng
* @date: 2016年3月3日 上午11:34:11
*
*/
public class Singleton2 {
private static Singleton2 instance;
private Singleton2 (){}
public static synchronized Singleton2 getInstance() {
if (instance == null) {
instance = new Singleton2();
}
return instance;
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 饿汉式
* @author: gao peng
* @date: 2016年3月3日 上午11:34:39
*
*/
public class Singleton3 {
private static Singleton3 instance = new Singleton3();
private Singleton3() {
}
public static Singleton3 getInstance() {
return instance;
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 双检锁/双重校验锁(DCL,即 double-checked locking)
* @author: gao peng
* @date: 2016年3月3日 上午11:35:21
*
*/
public class Singleton4 {
private volatile static Singleton4 Singleton4;
private Singleton4() {
}
public static Singleton4 getSingleton4() {
if (Singleton4 == null) {
synchronized (Singleton4.class) {
if (Singleton4 == null) {
Singleton4 = new Singleton4();
}
}
}
return Singleton4;
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 登记式/静态内部类
* @author: gao peng
* @date: 2016年3月3日 上午11:36:20
*
*/
public class Singleton5 {
private static class Singleton5Holder {
private static final Singleton5 INSTANCE = new Singleton5();
}
private Singleton5() {
}
public static final Singleton5 getInstance() {
return Singleton5Holder.INSTANCE;
}
}
/*
*Copyright (c) 2016, gp.inc and/or its affiliates. All rights reserved.
*/
package com.pattern.single;
/**
* 枚举
* @author: gao peng
* @date: 2016年3月3日 上午11:37:07
*
*/
public enum Singleton6 {
INSTANCE;
public void whateverMethod() {
}
}