设计模式 - 单件模式(singleton pattern) 详解
来源:互联网 发布:电子数据交换的好处 编辑:程序博客网 时间:2024/05/16 04:40
单件模式(singleton pattern) : 确保一个类只有一个实例, 并提供一个全局访问点.
单价模式包括3个部分: 私有构造器, 静态变量, 静态方法.
具体方法:
1. 标准的单例模式:
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class Singleton {
- private static Singleton uniqueInstance; //静态变量
- private Singleton() {} //私有构造函数
- public static Singleton getInstance() { //静态方法
- if (uniqueInstance == null)
- uniqueInstance = new Singleton();
- return uniqueInstance;
- }
- }
2. 考虑多线程的三种方法:
同步(synchronized)方法, 添加"synchronized", 会导致性能下降, 每次调用示例, 都需要同步, 但是使用简单.
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class Singleton {
- private static Singleton uniqueInstance; //静态变量
- private Singleton() {} //私有构造函数
- public static synchronized Singleton getInstance() { //静态方法
- if (uniqueInstance == null)
- uniqueInstance = new Singleton();
- return uniqueInstance;
- }
- }
急切(eagerly)方法, 开始时创建实例, 会在不需要时, 占用实例空间, 即占用空间时间过长.
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class Singleton {
- private static Singleton uniqueInstance = new Singleton(); //静态变量
- private Singleton() {} //私有构造函数
- public static synchronized Singleton getInstance() { //静态方法
- //if (uniqueInstance == null)
- //uniqueInstance = new Singleton();
- return uniqueInstance;
- }
- }
双重检查加锁(double-checked locking)方法, 使用"volatile"和"synchronized (Singleton.class)", 减少时间消耗, 适用于java1.4以上版本.
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class Singleton {
- private volatile static Singleton uniqueInstance; //静态变量
- private Singleton() {} //私有构造函数
- public static synchronized Singleton getInstance() { //静态方法
- if (uniqueInstance == null) {
- synchronized (Singleton.class) {
- if (uniqueInstance == null)
- uniqueInstance = new Singleton();
- }
- }
- return uniqueInstance;
- }
- }
3. 使用单件模式的例子:
代码:
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class ChocolateBoiler { //巧克力锅炉
- private boolean empty;
- private boolean boiled;
- public static ChocolateBoiler uniqueInstance; //静态变量
- private ChocolateBoiler() { //私有构造函数
- empty = true;
- boiled = false;
- }
- public static ChocolateBoiler getInstance() { //静态方法
- if (uniqueInstance == null)
- uniqueInstance = new ChocolateBoiler();
- return uniqueInstance;
- }
- public void fill() { //填满
- if (isEmpty()) {
- empty = false;
- boiled = false;
- }
- }
- public void drain() { //倾倒
- if (!isEmpty() && isBoiled())
- empty = true;
- }
- public void boil() { //煮
- if (!isEmpty() && !isBoiled()) {
- boiled = true;
- }
- }
- public boolean isEmpty() {
- return empty;
- }
- public boolean isBoiled() {
- return boiled;
- }
- }
4. 枚举单件(enum singleton)模式, 也可以保证线程安全.
代码:
- /**
- * @time 2014.6.5
- */
- package singleton;
- /**
- * @author C.L.Wang
- *
- */
- public class EnumSingleton {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- eSingleton d1 = eSingleton.INSTANCE;
- d1.setName("Spike");
- eSingleton d2 = eSingleton.INSTANCE;
- d2.setName("Caroline");
- System.out.println(d1);
- System.out.println(d2);
- System.out.println(d1 == d2);
- }
- }
- enum eSingleton {
- INSTANCE;
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return "[" + name + "]";
- }
- }
输出:
- [Caroline]
- [Caroline]
- true
0 0
- 设计模式 - 单件模式(singleton pattern) 详解
- 设计模式 - 单件模式(singleton pattern) 详解
- 设计模式-单件模式(Singleton Pattern)
- 单件模式(Singleton Pattern)
- 单件模式(Singleton Pattern)
- 单件模式(Singleton Pattern)
- 单件模式Singleton Pattern
- 单件模式Singleton Pattern
- 单件模式(Singleton Pattern
- 单件模式(Singleton Pattern)
- .NET设计模式-单件模式(Singleton Pattern)
- .NET设计模式:单件模式(Singleton Pattern)
- 设计模式之单件模式(Singleton Pattern )
- 单件模式(Singleton Pattern)-----设计模式学习
- .Net设计模式------单件模式(Singleton Pattern)
- C#设计模式之单件模式(Singleton Pattern)
- 设计模式之单件模式(Singleton Pattern)
- c++设计模式:单件模式(Singleton Pattern)
- winform网络编程之DNS类,IPAddress类和IPEndPoint类以及如何获取本地ip地址
- 新辰:90后五次创业经验分享:草根如何从零开始创业?
- MetaClass in Python
- 图像处理基础知识
- HDU 1233还是畅通工程——克鲁斯卡尔算法(带并查集)
- 设计模式 - 单件模式(singleton pattern) 详解
- 对正则表达式的理解
- 网站添加Google翻译代码
- SQL语句的添加、删除、修改多种方法
- 题目1008:最短路径问题(2010年浙江大学计算机及软件工程研究生机试真题)
- LeetCodeOJ--001(C++ && Python)
- STM32学习之路-FSMC与LCD
- 苹果Swift编程语言
- opencv学习博客