课堂笔记-单例模式
2013-07-14 19:52
176 查看
单例模式:构造方法私有化,对外提供一个公共的静态的访问入口。
class Singleton1
{
//饿汉式
//只有一个私有的构造方法
private Singleton1(){
System.out.println("Hello,singleton 1...");
}
private static final Singleton1 instance1 = new Singleton1();
//对外提供一个公共的静态的方法
public static Singleton1 getInstance(){
return instance1;
}
}
class Singleton2
{
//懒汉式
//只有一个私有的构造方法
private Singleton2(){
System.out.println("Hello,singleton 2...");
}
private static Singleton2 instance2 = null;
//对外提供一个公共的静态的方法
public static Singleton2 getInstance(){
if( instance2==null ){
instance2 = new Singleton2();
}
return instance2;
}
}
public class SingletonTest
{
public static void main(String[] args){
Singleton1.getInstance();
Singleton2.getInstance();
}
}
class Singleton1
{
//饿汉式
//只有一个私有的构造方法
private Singleton1(){
System.out.println("Hello,singleton 1...");
}
private static final Singleton1 instance1 = new Singleton1();
//对外提供一个公共的静态的方法
public static Singleton1 getInstance(){
return instance1;
}
}
class Singleton2
{
//懒汉式
//只有一个私有的构造方法
private Singleton2(){
System.out.println("Hello,singleton 2...");
}
private static Singleton2 instance2 = null;
//对外提供一个公共的静态的方法
public static Singleton2 getInstance(){
if( instance2==null ){
instance2 = new Singleton2();
}
return instance2;
}
}
public class SingletonTest
{
public static void main(String[] args){
Singleton1.getInstance();
Singleton2.getInstance();
}
}
相关文章推荐
- linux课堂笔记之二 开始命令模式与在线求助
- 课堂笔记-工厂模式
- 微软私有云的销售策略和License模式 MVA课堂笔记
- iOS UI 01 课堂笔记 -设计模式
- iOS UI 01 课堂笔记 -设计模式
- 设计模式课堂小笔记
- 微软私有云的销售策略和License模式 MVA课堂笔记
- 有关模式的课堂笔记
- JAVA设计模式之单例模式 - JAVA课堂笔记整理
- 设计模式之代理模式(也是马士兵上课课堂笔记)
- 科锐课堂笔记:2017/4/12 单例模式与运算符重载
- 组织行为学笔记:第六章 组织气氛与行为模式
- 设计模式 笔记 责任链模式 chain of responsibility
- 组合模式--设计模式解析与实战(关爱名 何红辉)笔记
- 设计模式 笔记 命令模式 Command
- 设计模式学习笔记之工厂模式
- 设计模式笔记(三)
- 设计模式(c++)笔记之二十(Visitor模式)
- 【面经笔记】linux I/O模式
- 设计模式笔记三:单例模式