设计模式(23种)分类:
创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。
行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。
单例模式:(注释:单例模式也称作单件模式)
1.定义 单例模式确保一个类只有一个实例,并提供一个全局访问点。
什么时候使用单例模式
线程池 缓存 对话框 处理偏好设置和注册表的对象 日志对象 充当显示器打印机等设备驱动程序的对象。
单例模式有饿汉式和懒汉式
饿汉式代码:
**
* 饿汉 ThreadSafe 关键点:静态变量初始化 浪费资源 天然线程安全 多线程环境下性能足够优秀
*/
public class SingletonHunger {
/**
* 类初始化(非实例化)时候创建对象,JVM 保证在任何线程访问该静态变量之前,一定会先创建此实例
*/
private static final SingletonHunger SINGLETON = new SingletonHunger();
private SingletonHunger() {
}
/**
* 获取对象,返回唯一实例
*/
public static SingletonHunger getInstance() {
return SINGLETON;
}
public void usefulMethods() {
}
}
懒汉式:
**
* 饿汉 ThreadSafe 关键点:静态变量初始化 浪费资源 天然线程安全 多线程环境下性能足够优秀
*/
public class SingletonHunger {
/**
* 类初始化(非实例化)时候创建对象,JVM 保证在任何线程访问该静态变量之前,一定会先创建此实例
*/
private static final SingletonHunger SINGLETON = new SingletonHunger();
private SingletonHunger() {
}
/**
* 获取对象,返回唯一实例
*/
public static SingletonHunger getInstance() {
return SINGLETON;
}
public void usefulMethods() {
}
}
/**
* 懒汉 UnThreadSafe 关键点:懒加载 线程不安全
*/
public class SingletonLazy {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy singleton;
private SingletonLazy() {
}
/**
* 调用该方法时初始化对象
*/
public static SingletonLazy getInstance() {
if (singleton == null) {
singleton = new SingletonLazy();
}
return singleton;
}
public void usefulMethods() {
}
}
懒汉式会导致线程不安全
/**
* 懒汉 UnThreadSafe 关键点:懒加载 线程不安全
*/
public class SingletonLazy {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy singleton;
private SingletonLazy() {
}
/**
* 调用该方法时初始化对象
*/
public static SingletonLazy getInstance() {
if (singleton == null) {
singleton = new SingletonLazy();
}
return singleton;
}
public void usefulMethods() {
}
}
懒汉变种一
/**
* 懒汉-变种1 ThreadSafe 关键点:懒加载、同步 线程安全 性能不强
*/
public class SingletonLazy_1 {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy_1 singleton = null;
private SingletonLazy_1() {
}
/**
* 调用该方法时初始化对象
*/
public static synchronized SingletonLazy_1 getInstance() {
if (singleton == null) {
singleton = new SingletonLazy_1();
}
return singleton;
}
public void usefulMethods() {
}
}
懒汉式变种二:
/**
* 懒汉-变种1 ThreadSafe 关键点:懒加载、同步 线程安全 性能不强
*/
public class SingletonLazy_1 {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy_1 singleton = null;
private SingletonLazy_1() {
}
/**
* 调用该方法时初始化对象
*/
public static synchronized SingletonLazy_1 getInstance() {
if (singleton == null) {
singleton = new SingletonLazy_1();
}
return singleton;
}
public void usefulMethods() {
}
}
/**
* 懒汉-变种2 UnThreadSafe 关键点:懒加载、DoubleCheckLock 线程不安全
*/
public class SingletonLazy_2 {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy_2 singleton = null;
private SingletonLazy_2() {
}
/**
* 调用该方法时初始化对象-双重检查锁-JDK 1.4版本及以下有些JVM无效
* !!! 注意,这里的双重检查锁有可能失效,singleton 对象并未能保证一致性,可能出现半个对象的问题
*/
public static SingletonLazy_2 getInstance() {
if (singleton == null) {
synchronized (SingletonLazy_2.class) {
if (singleton == null) {
singleton = new SingletonLazy_2();
}
}
}
return singleton;
}
public void usefulMethods() {
}
}
懒汉变种3
/**
* 懒汉-变种2 UnThreadSafe 关键点:懒加载、DoubleCheckLock 线程不安全
*/
public class SingletonLazy_2 {
/**
* 类加载时先不初始化对象
*/
private static SingletonLazy_2 singleton = null;
private SingletonLazy_2() {
}
/**
* 调用该方法时初始化对象-双重检查锁-JDK 1.4版本及以下有些JVM无效
* !!! 注意,这里的双重检查锁有可能失效,singleton 对象并未能保证一致性,可能出现半个对象的问题
*/
public static SingletonLazy_2 getInstance() {
if (singleton == null) {
synchronized (SingletonLazy_2.class) {
if (singleton == null) {
singleton = new SingletonLazy_2();
}
}
}
return singleton;
}
public void usefulMethods() {
}
}
/**
* 饱汉-变种3 ThreadSafe 关键点:懒加载、DoubleCheckLock、volatile关键字 线程安全 多线程环境下性能足够优秀
*/
public class SingletonLazy_3 {
/**
* 类加载时先不初始化对象
* 注意 volatile 关键字,确保该对象一致性,才能保证该模式在多线程下的线程安全
*/
private static volatile SingletonLazy_3 singleton = null;
private SingletonLazy_3() {
}
/**
* 调用该方法时初始化对象-双重检查锁-JDK 1.4版本及以下有些JVM无效
*/
public static SingletonLazy_3 getInstance() {
if (singleton == null) {
synchronized (SingletonLazy_3.class) {
if (singleton == null) {
singleton = new SingletonLazy_3();
}
}
}
return singleton;
}
public void usefulMethods() {
}
}
/**
* 饱汉-变种3 ThreadSafe 关键点:懒加载、DoubleCheckLock、volatile关键字 线程安全 多线程环境下性能足够优秀
*/
public class SingletonLazy_3 {
/**
* 类加载时先不初始化对象
* 注意 volatile 关键字,确保该对象一致性,才能保证该模式在多线程下的线程安全
*/
private static volatile SingletonLazy_3 singleton = null;
private SingletonLazy_3() {
}
/**
* 调用该方法时初始化对象-双重检查锁-JDK 1.4版本及以下有些JVM无效
*/
public static SingletonLazy_3 getInstance() {
if (singleton == null) {
synchronized (SingletonLazy_3.class) {
if (singleton == null) {
singleton = new SingletonLazy_3();
}
}
}
return singleton;
}
public void usefulMethods() {
}
}