Singleton: Ensure a class only has one instance and provide a global point of access to it.
code implementation
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}
}
We have a problem
The new Singleton code was running fine. The only thing we can think of is that we just added some optimizations to the Controller that makes use of multiple threads.
public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (uniqueInstance ==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}
}
I agree this fixes the problem. but synchronization is expensive.
it's actually a little worse than you make out; the only time synchronization is relevant is the first time through this method.
Can we improve multithreading?
1. Do nothing if the performance of getInstance() isnt critical to your application.
If calling the getInstance() method isnt causing substantial overhead for your application. forget about it. Keep in mind that synchronizing a method can decrease performance by a factor of 100, so if a high traffic part of your code begins using getInstance(), you may have to reconsider.
2 move to an eagerly created instance rather than a lazily created one.
if your application always creates and uses an instance of the Singleton or the overhead of creation and runtime aspects of Singleton are not oncrous, you may want to create your singleton eagerly.
public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return uniqueInstance;
}
}
Using this approach, we rely on the JVM to create the unique instance of the Singleton when the classs is loaded. The JVM guarantees that the instance will be created before any thread access the static uniqueInstance variable.
3 Use "double-checked locking" to reduce the use of synchronization in getInstance()
With double-checked locking, we first check to see if an instance is created, and if not, then we synchronize. This way, we only synchronize the first time through, just what we want.
Let's check out the code:
public class Singleton {
private volatile static Singleton uniqueInstance;
private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance==null) {
synchronized (Singleton.class) {
if (uniqueInstance==null)
uniqueInstance=new Singleton();
}
}
return uniqueInstance;
}
}
分享到:
相关推荐
singleton pattern 的定义 主要应用方法 优缺点 通过代码 具体分析解释
Singleton pattern单例模式应用
前端大厂最新面试题-Singleton Pattern.docx
Java据说有23种设计模式,Android的设计模式肯定是由Java来引申出来的。这里不讨论有多少人全会,有多少种设计模式会使用到,我们来讲下其中用得最多的也就是人人都知道的...这里是一个简单的SingletonPatternDemo。
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个...
4、单例模式(Singleton Pattern) 用意:仅允许生成一个对象时
Head First 设计模式 (五) 单件模式(Singleton pattern) C++实现
单例模式(Singleton Pattern) 保证整个系统中,对于某个对象,只能存在一个对象实例,该类值提供一个获取器对象的方法 当我们频繁的创建和销毁对象,会消耗大量资源,当我们使用对象通过new,将产生大量的对象实例,而单例...
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 学习demo
提供简单易懂的单件设计模式源码,为你学习这种模式提供更好捷径.
NULL 博文链接:https://lym6520.iteye.com/blog/695374
本文以实例形式讲述了C#单例模式(Singleton Pattern)的实现方法,分享给大家供大家参考。具体实现方法如下: 一般来说,当从应用程序全局的角度来看,如果只允许类的一个实例产生,就可以考虑单例模式。 1.即时加载...
主要为大家详细介绍了C#单例模式Singleton Pattern的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
3、单例模式SINGLETON PATTERN 4、多例模式MULTITION PATTERN 5、工厂方法模式FACTORY METHOD PATTERN 6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器模式ADAPTER PATTERN 9、模板...
Laravel开发-singleton-pattern 帮助程序包来实现单例类。
备忘录模式(Memento Pattern) 策略模式(Strategy Pattern) 抽象工厂模式(Abstract Factory Pattern) 代理模式(Proxy Pattern) 单例模式(Singleton Pattern) 迭代器模式(Iterator Pattern) 访问者模式(Visitor ...
单例模式[SINGLETON PATTERN] 多例模式[MULTITION PATTERN] 工厂方法模式[FACTORY METHOD PATTERN] 抽象工厂模式[ABSTRACT FACTORY PATTERN] 门面模式[FACADE PATTERN] 适配器模式[ADAPTER PATTERN] 模板方法模式...
Creating an Object Using the Singleton Pattern Using the Singleton Object Chapter 15. Model View Controller An Overview of the Pattern Creating the Pattern Using the Pattern Chapter 16. ...
单件模式(Singleton Pattern) 2. 抽象工厂(Abstract Factory) 3. 建造者模式(Builder) 4. 工厂方法模式(Factory Method) 5. 原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge ...