单例模式
来源:互联网 发布:淘宝可以说好评返现吗 编辑:程序博客网 时间:2024/05/17 03:06
构成单例模式重要的3点
1. 构造器声明为私有private
2. 声明静态变量, 指向自己的实例
3. 声明公有静态方法,返回自己的实例
饿汉式单例
- public class Singleton {
- private static Singleton singleton;
- private Singleton(){}
- public static synchronized Singleton getInstance(){
- if(singleton==null){
- singleton = new Singleton();
- }
- return singleton;
- }
- }
懒汉式单例
- public class Singleton {
- private static Singleton singleton;
- private Singleton(){}
- public static synchronized Singleton getInstance(){
- if(singleton==null){
- singleton = new Singleton();
- }
- return singleton;
- }
- }
0 0
- 单例、单例模式
- 单例模式-多线程单例模式
- 单件模式(单例模式)
- 设计模式------单例模式
- 设计模式------单例模式
- 设计模式-单例模式
- 设计模式 - 单例模式
- 设计模式---单例模式
- 设计模式---单例模式
- PHP模式-单例模式
- 【设计模式】单例模式
- 设计模式-单例模式
- 设计模式----单例模式
- 设计模式--单例模式
- 设计模式-单例模式
- 单例模式(单子模式)
- 设计模式-单例模式
- [设计模式] 单例模式
- hdu-5102
- create view
- 使用tomcat产生java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题
- iOS动画--UIView自带动画效果、Block动画
- 虚拟机入门
- 单例模式
- 学习笔记-SQL基础语法
- 两个背景图片滚动的效果
- hdu 2444The Accomodation of Students 二分匹配+dfs分组
- 第12周项目2 求N组数的最大公约数
- mini2440的串口在Qt上实现
- postgreSQL9.3日志配置
- DMA技术
- 刘首岐将演电影《谁的青春不叛逆》 叛逆少女转型成演员