zoukankan
html css js c++ java
Singleton模式的两种实现方法
在设计模式中,有一种叫Singleton模式的,用它可以实现一次只运行一个实例。就是说在程序运行期间,某个类只能有一个实例在运行。这种模式用途比较广泛,会经常用到,下面是Singleton模式的两种实现方法:
1、饿汉式
public
class
EagerSingleton
{
private
static
readonly
EagerSingleton instance
=
new
EagerSingleton();
private
EagerSingleton()
{}
public
static
EagerSingleton GetInstance()
{
return
instance;
}
}
2、懒汉式
public
class
LazySingleton
{
private
static
LazySingleton instance
=
null
;
private
LazySingleton()
{}
public
static
LazySingleton GetInstance()
{
if
(instance
==
null
)
{
instance
=
new
LazySingleton();
}
return
instance;
}
}
两种方式的比较:饿汉式在类加载时就被实例化,懒汉式类被加载时不会被实例化,而是在第一次引用时才实例化。这两种方法没有太大的差别,用哪种都可以。
查看全文
相关阅读:
全文搜索(AB-2)-权重
全文搜索(A-2)-推荐算法
全文检索(AB-1)-相关领域
全文搜索(A)-相关性
ElasticSearch全文搜索引擎(A)
mvc 的HtmlHelper
left join 与left outer join的区别
ms sqlserver数据库建索引
w3c xml
System and method for critical address space protection in a hypervisor environment
原文地址:https://www.cnblogs.com/michaelxu/p/679853.html
最新文章
Memcached Hash算法
JAVA虚拟机的类加载机制
JVM运行内存分配和回收
大数据de 2文章
关于评审--从思想到落地
从浏览器或者Webview 中唤醒APP
控制台的艺术(附原理实现)
浅谈代码结构的设计
POJ 3666 Making the Grade (DP)
POJ 3673 Cow Multiplication (水题)
热门文章
POJ 3672 Long Distance Racing (模拟)
POJ 3671 Dining Cows (DP,LIS, 暴力)
POJ 3670 Eating Together (DP,LIS)
POJ 3369 Meteor Shower (BFS,水题)
POJ 3668 Game of Lines (暴力,判重)
UVaLive 7269 Snake Carpet (找规律,模拟)
UVaLive 7267 Mysterious Antiques in Sackler Museum (if-else,枚举)
HDU 4463 Outlets (最小生成树)
全文索引(A-1)-用户数据收集(用户研究)
全文搜索(AC-1)-互联网信息过载问题
Copyright © 2011-2022 走看看