单例模式
来源:互联网 发布:java系统日志记录 编辑:程序博客网 时间:2024/06/04 01:05
常见的单例模式
懒汉式(不安全)
public class Singleton{ private Singletion(){} private static Singleton Instance; public static Singeton getInstance(){ if(Instance==null){ Instance = new Singleton(); } return Instance; }}
懒汉式(安全低效率)
public class Singleton{ private Singletion(){} private static Singleton Instance; public static Synchronizeed Singeton getInstance(){ if(Instance==null){ Instance = new Singleton(); } return Instance; }}
恶汉式
public class Singleton{ private Singletion(){} private static Singleton Instance = new Singleton(); public static Singeton getInstance(){ return Instance; }}
懒汉式(双校验)
public class Singleton{ private Singletion(){} private static Singleton Instance; public static Singeton getInstance(){ if(Instance==null){ Synchronizeed(Singleton.class){ if(Instance==null){ Instance = new Singleton(); } } } return Instance; }}
懒汉式(静态式)
public class Singleton{ private Singletion(){} private static final class SingletonHolder { private final Singleton instance = new Singleton(); } public static Singeton getInstance(){ return SingletonHolder.instance; }}
枚举
public enum Singleton{ Instance; public void doMethod(){};}
阅读全文
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- linux 基础学习5
- 模块化编程
- 内存映射MMAP和DMA
- Kth Smallest Number in Multiplication Table
- 30. Substring with Concatenation of All Words
- 单例模式
- PyCharm20171025测试激活码可用http://xidea.online
- 1025-hadoop课程
- 设计模式——适配器模式
- LeetCode52. N-Queens II
- sudo xxxx is not in the sudoers file
- 强化学习(一)~(五)
- Linux的总线、设备、驱动模型
- 264. Ugly Number II