单例详解
来源:互联网 发布:淘宝怎么设置客服接待 编辑:程序博客网 时间:2024/05/29 04:58
2 public class Singleton1 {
3 //私有的默认构造子
4 private Singleton1() {}
5 //已经自行实例化
6 private static final Singleton1 single = new Singleton1();
7 //静态工厂方法
8 public static Singleton1 getInstance() {
9 return single;
10 }
11 }
(2)懒汉模式://懒汉式单例类.在第一次调用的时候实例化
2 public class Singleton2 {
3 //私有的默认构造子
4 private Singleton2() {}
5 //注意,这里没有final
6 private static Singleton2 single=null;
7 //静态工厂方法
8 public synchronized static Singleton2 getInstance() {
9 if (single == null) {
10 single = new Singleton2();
11 }
12 return single;
13 }
14 }
(3)工厂模式:
interface IFactory{
public IProduct createProduct();}
Class Factory implements IFactory{
public IProduct createProduct(){return new Product();}}
Public class client{
Public Static void main (String [] args){IFactory factory=new Factory();
IProduct product=factory.createProduct();
product.ProductMethod();
}}
- 单例模式详解
- 单例模式详解
- 单例模式详解
- 单例模式详解
- 单例模式详解
- 单例模式详解
- 单例模式详解
- java单例详解
- JAVA单例详解
- 单例模式详解
- cocos2dx 单例详解
- 单例模式详解
- 详解单例模式
- 单例模式详解
- 单例模式详解
- 详解【单例模式】
- 单例模式 详解
- 单例模式详解
- 危桥僵尸和创新思维
- Hive的分桶详解
- 数据结构8————串的BF匹配模式和KMP匹配模式
- 关键字--exists用法
- [USACO 2013 Jan]Island Travels
- 单例详解
- 抽象工厂方法
- 开发日记 X
- 排列(康托展开)
- 理解与配置Android studio中的gradle
- 数据结构: 单链表排序
- “删除重复元素”:保留两个
- [队内测试Day10.22T1][bzoj1821]部落划分 Group
- JZ2440 第7章 内存管理单元 MMU