单例模式的几种实现方式
来源:互联网 发布:东方的漫画软件 编辑:程序博客网 时间:2024/06/05 02:53
/**考察知识点单例及Spring Bean的单例设计*/
class Singleton01{
private Singleton01() {}
private static Singleton01 instance;
public static Singleton01 getInstance() {
if(instance==null) {
instance=new Singleton01();
}
return instance;
}
}
class Singleton02{//适合大对象,稀少用
private Singleton02() {}
private static Singleton02 instance;
public synchronized static Singleton02 getInstance() {
if(instance==null) {
instance=new Singleton02();
}
return instance;
}
}
class Singleton03{//适合大对象(合适需要合适加载),稀少用
private Singleton03() {}
private static volatile Singleton03 instance;
public static Singleton03 getInstance() {
if(instance==null) {
synchronized (Singleton03.class) {
if(instance==null) {
instance=new Singleton03();
}
}
}
return instance;
}
}
class Singleton04{//小对象,频繁用
//int[] n=new int[1024];
private Singleton04() {}
private static Singleton04 instance=new Singleton04();
public static Singleton04 getInstance() {
return instance;
}
public static void display() {}
}
class Singleton05{//对04进行优化
private Singleton05() {}
static class Inner{//延迟加载策略
private static Singleton05 instance=
new Singleton05();
}
public static Singleton05 getInstance() {
return Inner.instance;
}
public static void display() {}
public void show() {}
}
enum Singleton06{
instance;
public static void display() {}
}
class Singleton01{
private Singleton01() {}
private static Singleton01 instance;
public static Singleton01 getInstance() {
if(instance==null) {
instance=new Singleton01();
}
return instance;
}
}
class Singleton02{//适合大对象,稀少用
private Singleton02() {}
private static Singleton02 instance;
public synchronized static Singleton02 getInstance() {
if(instance==null) {
instance=new Singleton02();
}
return instance;
}
}
class Singleton03{//适合大对象(合适需要合适加载),稀少用
private Singleton03() {}
private static volatile Singleton03 instance;
public static Singleton03 getInstance() {
if(instance==null) {
synchronized (Singleton03.class) {
if(instance==null) {
instance=new Singleton03();
}
}
}
return instance;
}
}
class Singleton04{//小对象,频繁用
//int[] n=new int[1024];
private Singleton04() {}
private static Singleton04 instance=new Singleton04();
public static Singleton04 getInstance() {
return instance;
}
public static void display() {}
}
class Singleton05{//对04进行优化
private Singleton05() {}
static class Inner{//延迟加载策略
private static Singleton05 instance=
new Singleton05();
}
public static Singleton05 getInstance() {
return Inner.instance;
}
public static void display() {}
public void show() {}
}
enum Singleton06{
instance;
public static void display() {}
}
阅读全文
0 0
- 单例模式几种实现方式
- 单例模式的几种实现方式的对比
- 单例模式singleton的几种实现方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式比较
- C#单例模式的几种实现方式
- 单例模式的几种实现方式总结
- 常用设计模式-单例的几种实现方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式
- Java单例模式的几种实现方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式
- 单例模式实现的几种方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式
- 单例模式的几种实现方式
- transform: translateY(-50%) 实现元素垂直居中效果
- bzoj 2962: 序列操作 线段树
- 如何使用java代码自动生成对应的krpano全景
- 冯诺依曼结构和哈佛结构
- MySQL锁机制
- 单例模式的几种实现方式
- leetcode 75. Sort Colors 很不错的3种元素排序方法 + O(n)
- A Simple Problem with Integers 区间更新和查询
- 设计模式学习笔记(工厂模式)
- EF映射
- 最长上升子序列(LIS)
- 基于Tensorflow实现基本的线性回归(Linear regression)
- hdu 6197array array array(最长不下降子序列nlogn)
- 07-异常