当前位置:网站首页>单例模式的实现
单例模式的实现
2022-06-13 09:25:00 【edui】
- 定义
这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。 - 实现
public class Singleton{
//一个私有的静待变量保存类的实例,不可直接访问和修改
private static Singleton instance= new Singleton();
//私有化构造函数,不可以被外部条用创建实例
private Singleton(){
}
//唯一接口获取单例的对象
public static Singleton getInstance(){
return instance;
}
/** *类的其他成员 **/
}
首先将构造函数房屋内控制为private ,只能为类内调用实例化,这样其他类就没办法实例化该对象了。
其次,定义一个保存唯一实例的变量,必须为类变量,用于保存唯一实例对象。
接下来提供一个接口用于获取该唯一实例。
比如该类,static修饰的类变量instance在类加载初始化阶段就得到一个实例,保存在方法区,执行堆中唯一的实例对象,由于直接保存在方法区,又提供一个静态的方法访问方法所以每次访问该类的实例都会获取到保存方法区的类变量instance值,而该值指向堆中的一个对象,类变量instance值值是唯一的(因为类在方法区是为唯一的)所以实例也是唯一的。
上面的方法线程安全的,但是只要类加载了就不管用不用都会创建一个对象,浪费空间即不支持 Lazy 初始化,所以就产生了一下支持Lazy初始化的实现方法。
——————————————————————————————————————————————
public class Singleton{
//一个私有的静待变量保存类的实例,不可直接访问和修改
private volatile static Singleton instance;
//私有化构造函数,不可以被外部条用创建实例
private Singleton(){
}
//唯一接口获取单例的对象
public static Singleton getInstance(){
//使用时如果不存在则先初始化,由于为了保证只创建一个对象,进行加同步锁
if(instance = null){
synchronized(Singleton.class){
if(instance = null)
instance = new Singleton;
}
}
return instance;
}
/** *类的其他成员 **/
}
这种实现方式在需要获取对象的时候才对类进行实例化,即实现了Lazy初始化,为了线程安全即无论几个线程同时访问都只能实例化一个对象需要加同步锁,实例对象的类变量需要用volatile修饰保证一个线程实例化后其他线程可见 。
这种方式虽然实现了Lazy初始化但是由于加同步锁会影响一些性能,鱼和熊不可兼得也
————————————————————————————————————————
还有一种常见的实现方法是,通过建立内部类,只有当使用到时内部类才会被加载,即通过延迟类的加载来实现Lazy初始化的作用,又是对线程安全的
public class Singleton {
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){
}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
边栏推荐
- C language: callback function
- [51nod p2102] or subtraction and [bit operation]
- (state compression dp+ binary) acwing 91 Shortest Hamilton path
- acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
- Jenkins集成Ldap,Ldap配置错误导致jenkins用户登录失败问题解决
- Learning makefile with me
- [poj1845] sumdiv [number theory]
- VGA common resolution and calculation method
- Instruction level parallelism (?)
- C language: sanziqi
猜你喜欢
![[51nod p3058] Xiao ming'ai set [set]](/img/a8/dbe6d4ed5881a757780700143e0526.jpg)
[51nod p3058] Xiao ming'ai set [set]

Classes and objects -- Inheritance

Jenkins access openldap user authentication

Trees and binary trees: Construction of binary trees
![[Luogu p1403] Research on divisor](/img/c1/b2a91ddad264d5f8283abf27dc8839.jpg)
[Luogu p1403] Research on divisor

C language: five custom types
![1-4 message passing interface [CSP authentication]](/img/db/aecda548693cdfb0e740bcf1a1c823.jpg)
1-4 message passing interface [CSP authentication]

Acwing785. quick sort (sort+ quick sort + merge sort)

Classes and objects -- polymorphic

Heap
随机推荐
Storage mode of drawings
Exercise 8-3 rotate the array to the right (20 points)
A static variable is associated with a class and can be used as long as the class is in memory (the variable does not exist as long as your application terminates). (heap body, stack reference)
I have summarized the knowledge points of JS [intermediate and advanced] for you
Summary of random number learning
VDD, dvdd, avdd, VCC, afvdd, dovdd, iovdd
(bfs+GOOD) acwing 845. Eight digit
LeetCode 5259. 计算应缴税款总额
[51nod p2653] interval XOR [bit operation]
(topological sorting +bfs) acwing 848 Topological sequence of digraph
MOOC week 8 programming exercise 1
@Value不生效及extend/implement其他类无法注入bean的手动注入
C language: deep understanding of pointers and arrays
acwing 789. Range of numbers (dichotomy + suitable for understanding dichotomy boundary)
VDD,DVDD,AVDD,VCC,AFVDD,DOVDD,IOVDD
LeetCode 6097. Match after replacing characters (Dictionary)
(dfs) acwing 842. Arrange numbers
[Luogu p1090, ssl1040] merged fruit [pile]
[ssl1280] full arrangement
Consolas-with-Yahei