设计模式--创建模式--单例模式
一、基本概念
只要掌握:
1、 单例模式类只能有一个实例
2、 单例模式类必须自己创建自己的唯一实例
3、 单例模式类必须给所有其他对象提供这一实例
二、例子
1、单例:非线程安全懒汉加载
1 package comm.pattern.create.singleton; 2 3 /** 4 * 5 * @Title: Singleton1.java 6 * @Package: comm.pattern.create.singleton 7 * @Description: 懒汉单例模式 8 * @author yangzhancheng 9 * @2020年3月27日:上午1:03:34 10 * 11 */ 12 public class SingletonDelay { 13 14 /** 15 * 该函数限制用户主动创建实例 16 */ 17 private SingletonDelay() { 18 19 } 20 21 private static SingletonDelay singleton = null; 22 23 /** 24 * 获取Singleton实例(也叫静态工厂方法) 25 * @return Singleton 26 */ 27 public static SingletonDelay getSingleton() { 28 /* 当singleton为空时创建它,反之直接返回,保证唯一性 */ 29 if(singleton == null){ 30 singleton = new SingletonDelay(); 31 } 32 return singleton; 33 } 34 35 public void out(){ 36 System.out.println("1、单例:非线程安全懒汉加载..."); 37 } 38 39 }
2、单例:线程安全懒汉加载
1 package comm.pattern.create.singleton; 2 3 /** 4 * 5 * @Title: Singleton1.java 6 * @Package: comm.pattern.create.singleton 7 * @Description: 懒汉单例模式(线程安全) 8 * @author yangzhancheng 9 * @2020年3月27日:上午1:03:34 10 * 11 */ 12 public class SingletonDelaySyn { 13 14 /** 15 * 该函数限制用户主动创建实例 16 */ 17 private SingletonDelaySyn() { 18 19 } 20 21 private static SingletonDelaySyn singleton = null; 22 23 /** 24 * 获取Singleton实例(也叫静态工厂方法) 25 * @return Singleton 26 */ 27 public static synchronized SingletonDelaySyn getSingleton() { 28 /* 当singleton为空时创建它,反之直接返回,保证唯一性 */ 29 if(singleton == null){ 30 singleton = new SingletonDelaySyn(); 31 } 32 return singleton; 33 } 34 35 public void out(){ 36 System.out.println("2、单例:线程安全懒汉加载..."); 37 } 38 39 40 }
3、单例:内部静态类懒汉加载
1 package comm.pattern.create.singleton; 2 3 /** 4 * 5 * @Title: Singleton1.java 6 * @Package: comm.pattern.create.singleton 7 * @Description: 懒汉单例模式(线程安全) 8 * @author yangzhancheng 9 * @2020年3月27日:上午1:03:34 10 * 11 */ 12 public class SingletonDelaySynDouble { 13 14 private static class LazyHolder { 15 // 创建Singleton实例 16 private static final SingletonDelaySynDouble INSTANCE = new SingletonDelaySynDouble(); 17 } 18 19 /** 20 * 该函数限制用户主动创建实例 21 */ 22 private SingletonDelaySynDouble() { 23 24 } 25 26 /** 27 * 获取Singleton实例,也叫静态工厂方法 28 * @return Singleton 29 */ 30 public static final SingletonDelaySynDouble getSingleton() { 31 return LazyHolder.INSTANCE; 32 } 33 34 35 public void out(){ 36 System.out.println("3、单例:内部静态类懒汉加载..."); 37 } 38 39 }
4、单例:双线程安全懒汉加载
1 package comm.pattern.create.singleton; 2 3 /** 4 * 5 * @Title: Singleton1.java 6 * @Package: comm.pattern.create.singleton 7 * @Description: 懒汉单例模式(线程安全) 8 * @author yangzhancheng 9 * @2020年3月27日:上午1:03:34 10 * 11 */ 12 public class SingletonDelaySynStatic { 13 14 /** 15 * 该函数限制用户主动创建实例 16 */ 17 private SingletonDelaySynStatic() { 18 19 } 20 21 private volatile static SingletonDelaySynStatic singleton = null; 22 23 24 /** 25 * 获取Singleton实例(也叫静态工厂方法) 26 * @return Singleton 27 */ 28 public static SingletonDelaySynStatic getSingleton() { 29 /* 当singleton为空时创建它,反之直接返回,保证唯一性 */ 30 if (singleton == null) { 31 synchronized (SingletonDelaySynStatic.class) { 32 if (singleton == null) { 33 singleton = new SingletonDelaySynStatic(); 34 } 35 } 36 } 37 return singleton; 38 } 39 40 public void out(){ 41 System.out.println("4、单例:双线程安全懒汉加载..."); 42 } 43 44 }
5、单例:饿汉加载
1 package comm.pattern.create.singleton; 2 3 /** 4 * 5 * @Title: Singleton1.java 6 * @Package: comm.pattern.create.singleton 7 * @Description: 饿汉单例模式 8 * @author yangzhancheng 9 * @2020年3月27日:上午1:03:34 10 * 11 */ 12 public class SingletonHungry { 13 14 /** 15 * 该函数限制用户主动创建实例 16 */ 17 private SingletonHungry() {} 18 19 private static final SingletonHungry singleton = new SingletonHungry(); 20 21 /** 22 * 获取Singleton实例,也叫静态工厂方法 23 * @return Singleton 24 */ 25 public static SingletonHungry getSingleton() { 26 return singleton; 27 } 28 29 public void out(){ 30 System.out.println("5、单例:饿汉加载..."); 31 } 32 33 }
测试类
1 package comm.pattern.create.singleton; 2 3 import java.util.HashMap; 4 5 /** 6 * 7 * @Title: Client.java 8 * @Package: comm.pattern.create.singleton 9 * @Description: 描述该文件做什么 10 * @author yangzhancheng 11 * @2020年3月27日:上午1:17:47 12 * 13 */ 14 public class Client { 15 16 public static void main(String[] args) { 17 18 //1、单例:非线程安全懒汉加载 19 SingletonDelay singletonDelay = SingletonDelay.getSingleton(); 20 singletonDelay.out(); 21 22 //2、单例:线程安全懒汉加载 23 SingletonDelaySyn singletonDelaySyn = SingletonDelaySyn.getSingleton(); 24 singletonDelaySyn.out(); 25 26 //3、单例:内部静态类懒汉加载 27 SingletonDelaySynDouble singletonDelaySynDouble = SingletonDelaySynDouble.getSingleton(); 28 singletonDelaySynDouble.out(); 29 30 //4、单例:双线程安全懒汉加载 31 SingletonDelaySynStatic singletonDelaySynStatic = SingletonDelaySynStatic.getSingleton(); 32 singletonDelaySynStatic.out(); 33 34 //5、单例:饿汉加载 35 SingletonHungry singletonHungry = SingletonHungry.getSingleton(); 36 singletonHungry.out(); 37 } 38 39 }
运行结果
1、单例:非线程安全懒汉加载... 2、单例:线程安全懒汉加载... 3、单例:内部静态类懒汉加载... 4、单例:双线程安全懒汉加载... 5、单例:饿汉加载...
三、总结
使用比较多,后续总结吧。