单例模式 - 创建者模式

来源:互联网 发布:数据录入项目外包 编辑:程序博客网 时间:2024/05/17 07:07

个人理解:

模式类型:
Singleton 单例模式 - 创建者模式

意图:

Ensure that only one instance of a class is created.
Provide a global point of access to the object.

Singleton单例模式目的在于确保一个类只有唯一的一个实例,并且这个唯一实例只有一个全局访问点。它确保类被实例一次,所有这个类的请求都指向这个唯一的实例。

概述:

角色:

模式的应用场景:
1、确保类仅存在唯一的实例
2、控制访问这个实例在系统中非常关键
3、在类实例以后的会多次使用这个实例
4、由实例化的类来控制单例,而不是由其他机制来控制。

Use the Singleton pattern when …
1 You need to ensure there is only one instance of a class.
2 Controlled access to that instance is essential.
3 You might need more than one instance at a later stage.
4 The control should be localized in the instantiated class, not in some other mechanism.

PS: 频繁访问数据库或文件的对象。
例如资源管理器、打印机、通信端口等。
单例模式应用的场景一般发现在以下条件下:
(1)资源共享的情况下,避免由于资源操作时导致的性能或损耗等。如上述中的日志文件,应用配置。
(2)控制资源的情况下,方便资源之间的互相通信。如线程池等。

结构图:
这里写图片描述

模式的优缺点:

代码:
1、饿汉

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

2、静态内部类

public class Singleton {          private static class SingletonHolder {          private static final Singleton INSTANCE = new Singleton();          }          private Singleton (){}          public static final Singleton getInstance() {          return SingletonHolder.INSTANCE;          }      }  

3、枚举【1.线程安全 2.不会因为序列化而产生新实例 3.防止反射攻击】
再具体点的例子,有空再找吧

public enum Singleton {          INSTANCE;          public void whateverMethod() {          }      }  

4、双重校验锁

public class Singleton {          private volatile static Singleton singleton;          private Singleton (){}          public static Singleton getSingleton() {          if (singleton == null) {              synchronized (Singleton.class) {              if (singleton == null) {                  singleton = new Singleton();              }              }          }          return singleton;          }      }  

Feature:从Java1.5开始支持;无偿提供序列化机制,绝对防止多次实例化,即使在面对复杂的序列化或者反射攻击的时候。

总之,五类:懒汉,恶汉,双重校验锁,静态内部类,枚举。
恶汉:因为加载类的时候就创建实例,所以线程安全(多个ClassLoader存在时例外)。缺点是不能延时加载。
懒汉:需要加锁才能实现多线程同步,但是效率会降低。优点是延时加载。
双重校验锁:麻烦,在当前Java内存模型中不一定都管用,某些平台和编译器甚至是错误的,因为instance = new MaYun()这种代码在不同编译器上的行为和实现方式不可预知。
静态内部类:延迟加载,减少内存开销。因为用到的时候才加载,避免了静态field在单例类加载时即进入到堆内存的permanent代而永远得不到回收的缺点(大多数垃圾回收算法是这样)。
枚举:很好,不仅能避免多线程同步问题,而且还能防止反序列化重新创建新的对象。但是失去了类的一些特性,没有延迟加载,用的人也太少了~~

所有模式:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。

参考/转自:
http://cantellow.iteye.com/blog/838473 【单例的七种写法】
http://blog.csdn.net/horace20/article/details/37562513 【使用枚举的例子】
http://blog.csdn.net/java2000_net/article/details/3983958 【证明枚举的可靠性】
http://www.tuicool.com/articles/AjeEfq 【作者自己创建了一个枚举在c3p0应用中】
http://blog.csdn.net/paincupid/article/details/46689163 【看了一下c3p0的源代码,其它他没有用枚举的】
http://www.oodesign.com/singleton-pattern.html

转载请注明:
http://blog.csdn.net/paincupid/article/details/46689193

0 0
原创粉丝点击