1. 模式介绍 模式的定义 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。 模式的使用场景 确保某个类有且只有一个对象的场景,例如创建一个对象需要消耗的资源过多,如要访问 IO 和数据库等资源。 2. UML类图  角色介绍: (1)Client : 高层客户端。 (2)Singleton : 单例类。 3. 模式的简单实现 - public class Singleton {
- private static Singleton intance;
- private Singleton() {}
-
- public static Singleton getInstance() {
- /*
- * 一开始多线程进来,遇到锁,一个线程进去,是为空,new对象; 后续线程进入,不为空,不操作;最后直接返回
- * 对象不为空,再有多个线程进入该函数,不为空,不执行加锁操作,直接返回
- */
- if (intance == null) {
- synchronized (Singleton.class) {
- if (intance == null) {
- intance = new Singleton();
- }
- }
- }
- return intance;
- }
- }
-
- class Singleton1 {// 懒汉式
- private static Singleton1 intance = new Singleton1();//懒的,程序运行的时候就加载出来了
- private Singleton1() {}
-
- public static Singleton1 getInstance() {
- return intance;
- }
- }
-
- class Singleton2 {// 饿汉式
- private static Singleton2 intance;
- private Singleton2() {}
-
- public static Singleton2 getInstance() {//用到的时候 才加载
- if (intance == null) {
- intance = new Singleton2();
- }
- return intance;
- }
- }
-
- class Singleton3 {// 饿汉式 线程安全
- private static Singleton3 intance;
- private Singleton3() {}
-
- public synchronized static Singleton3 getInstance() {//用到的时候 才加载, 加锁 多线程调用,都有一个加锁的动作
- if (intance == null) {
- intance = new Singleton3();
- }
- return intance;
- }
- }
-
- class Singleton4 {// 饿汉式 线程安全
- private static Singleton4 intance;
- private Singleton4() {}
-
- public static Singleton4 getInstance() {//用到的时候 才加载
- synchronized (Singleton4.class) {// 加锁 效率跟3差不多
- if (intance == null) {
- intance = new Singleton4();
- }
- }
- return intance;
- }
- }
复制代码 4.优点与缺点 (1)优点: A.由于单例模式在内存中只有一个实例,减少了内存开支,特别是一个对象需要频繁地创建、销毁时,而且创建或销毁时性能又无法优化,单例模式的优势就非常明显。 B.由于单例模式只生成一个实例,所以减少了系统的性能开销,当一个对象的产生需要比较多的资源时,如读取配置、产生其他依赖对象时,则可以通过在应用启动时直接产生一个单例对象,然后用永久驻留内存的方式来解决; C.单例模式可以避免对资源的多重占用,例如一个写文件动作,由于只有一个实例存在内存中,避免对同一个资源文件的同时写操作。 D.单例模式可以在系统设置全局的访问点,优化和共享资源访问,例如可以设计一个单例类,负责所有数据表的映射处理。 (2)缺点 A.单例模式一般没有接口,扩展很困难,若要扩展,除了修改代码基本上没有第二种途径可以实现。 |