`

Singleton design pattern

阅读更多
定义:
Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在。

第一种形式:
public class Singleton{

    private Singleton(){};
    private static singleton = new Singleton();
    public staic Singleton getInstance(){
       return singleton;
    }
}
第二种形式:

public class Singleton{

   private Singleton singleton = null;
   public static synchronized Singleton getInstance(){
      if(singleton == null){
         singleton = new Singleton();
      }
      return singleton;
   }
}
    第二种形式是lazy initialization,也就是说第一次调用时初始Singleton,以后就不用再生成了。
    注意到lazy initialization形式中的synchronized,这个synchronized很重要,如果没有synchronized,那么使用getInstance()是有可能得到多个Singleton实例。

使用Singleton注意事项:
    有时在某些情况下,使用Singleton并不能达到Singleton的目的,如有多个Singleton对象同时被不同的类装入器装载;在EJB这样的分布式系统中使用也要注意这种情况,因为EJB是跨服务器,跨JVM的。
    Singleton模式看起来简单,使用方法也很方便,但是真正用好,是非常不容易,需要对Java的类、线程、内存等概念有相当的了解。





分享到:
评论

相关推荐

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    36种最新设计模式整理

    Design Pattern: Singleton 模式 Design Pattern: Registry of Singleton 模式 Design Pattern: Default Adapter 模式 31 Design Pattern: Adapter 模式 - Object Adapter 32 Design Pattern: Adapter 模式 - ...

    Java.Design.Patterns.1537192353

    SINGLETON PATTERN FACTORY PATTERN ABSTRACT FACTORY PATTERN BUILDER PATTERN PROTOTYPE PATTERN ADAPTER PATTERN COMPOSITE PATTERN PROXY PATTERN FAÇADE PATTERN DECORATOR PATTERN FLYWEIGHT PATTERN ...

    Python Cookbook英文版

    5.22 Avoiding the Singleton Design Pattern with the Borg Idiom 5.23 Implementing the Null Object Design Pattern 6. Threads, Processes, and Synchronization 6.1 Storing Per-Thread Information 6.2...

    项目实训之-银行系统

    -the banking system relates to multi-threaded, socket programming, and Singleton design pattern. Notes for the complete source Gilmour, documentation, together with the development process UML design....

    Design.Patterns.Explained.Simply

    Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento ...

    design-pattern-java.pdf

    工厂三兄弟之抽象工厂模式(二) 工厂三兄弟之抽象工厂模式(三) 工厂三兄弟之抽象工厂模式(四) 工厂三兄弟之抽象工厂模式(五) 单例模式-Singleton Pattern 确保对象的唯一性——单例模式 (一) 确保对象的...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    C# Game Programming Cookbook for Unity 3D - 2014

    1.1.3 Using the Singleton Pattern in Unity...........................5 1.1.4 Inheritance.................................................6 1.1.5 Where to Now?.............................................

    设计模式 design pattern

    3.5 Singleton(单件)—对象创建型 模式 84 3.6 创建型模式的讨论 89 第4章 结构型模式 91 4.1 Adapter(适配器)—类对象结构型 模式 92 4.2 Bridge(桥接)—对象结构型 模式 100 4.3 Composite(组成)—对象...

    C++ 实现的singleton 模式

    用VC实现的singleton 模式 在VS03,VC6.0下编译通过

    Design Patterns Elements of Reusable Object-Oriented Software

    • What Is a Design Pattern? • Design Patterns in Smalltalk MVC • Describing Design Patterns • The Catalog of Design Patterns • Organizing the Catalog • How Design Patterns Solve Design ...

    Apress.Pro.Design.Patterns.in.Swift

    The Singleton Pattern Chapter 7. The Object Pool Pattern Chapter 8. Object Pool Variations Chapter 9. The Factory Method Pattern Chapter 10. The Abstract Factory Pattern Chapter 11. The Builder ...

    DesignPattern_Singleton:csdb博客设计模式(1)_单例模式原始码

    DesignPattern_Singleton csdb博客设计模式(1)_单例模式原始码 CSDN博客地址

    Learning Python Design Patterns(PACKT,2013)

    Then you will move on to learn about two creational design patterns which are Singleton and Factory, and two structural patterns which are Facade and Proxy. Finally, the book also explains three ...

    Design Patterns in Modern C++--2018

    Use creational patterns of builder, factories, prototype and singleton Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as ...

    CoreJava-DesignPattern

    CoreJava-DesignPattern 创意设计模式 -- Abstract Factory - Done -- Builder - Done -- Factory Method -- Object Pool -- Prototype - Done -- Singleton - Done 结构设计模式 -- Adapter -- Bridge -- ...

    深入浅出设计模式中文

    5 One of a Kind Objects: the Singleton Pattern 169 6 Encapsulating Invocation: the Command Pattern 191 7 Being Adaptive: the Adapter and Facade Patterns 235 8 Encapsulating Algorithms: theTemplate ...

Global site tag (gtag.js) - Google Analytics