Java Singleton:

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine.

The singleton class must provide a global access point to get the instance of the class.
Singleton pattern is used for logging, drivers objects, caching and thread pool.

Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc. Singleton design pattern is used in core java classes also,for example, java.lang.Runtime, java.awt.Desktop.

Java Singleton pattern

To implement Singleton pattern, there are different approaches but all of them have following common concepts.

1. Private constructor to restrict instantiation of the class from other classes.
2. Private static variable of the same class that is the only instance of the class.
3. Public static method that returns the istance of the class, this is the global access point for outer would to get the instance of the singleton class.

Different approaches of Singleton pattern implementation and design concerns with the implementation.

  1. Eager initialization
  2. Static block initialization
  3. Lazy Initialization
  4. Thread Safe Singleton
  5. Bill Pugh Singleton Implementation
  6. Using Reflection to destroy Singleton Pattern
  7. Enum Singleton
  8. Serialization and Singleton

Eager initialization:
In eager initialization, the instance of Singleton Class is created at the time of class loading, this is the easiest method to create a singleton class but it has a drawback that instance is created even though client application might not be using it.

public class EagerInitializedSingleton{
      private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
      //private constructor to avoid client applications to use constructor
      private EagerInitializedSingleton(){}
      public static EagerInitializedSingleton getInstance(){
              return instance;
        }
}

Singleton Summary的更多相关文章

  1. Lintcode: Singleton && Summary: Synchronization and OOD

    Singleton is a most widely used design pattern. If a class has and only has one instance at every mo ...

  2. C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)

    一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...

  3. Net设计模式实例之单例模式( Singleton Pattern)

    一.单例模式简介(Brief Introduction) 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯 ...

  4. 【转】单例模式(Singleton)

    首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...

  5. 设计模式(一)单例模式(Singleton Pattern)

    一.引言 最近在设计模式的一些内容,主要的参考书籍是<Head First 设计模式>,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了 ...

  6. 跨应用程序域(AppDomain)的单例(Singleton)实现

    转载自: 跨应用程序域(AppDomain)的单例(Singleton)实现 - CorePlex代码库 - CorePlex官方网站,Visual Studio插件,代码大全,代码仓库,代码整理,分 ...

  7. 设计模式:单例模式(Singleton)

    定义:确保一个类仅有一个实例,并提供一个访问它的全局访问点. 优点:在内存中只有一个对象,节省了内存空间 示例: Singleton.cs 写法一:非线程安全 public class Singlet ...

  8. 单例模式(Singleton)的6种实现

    1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...

  9. 1、Singleton 单件(创建模式)

    一.Singleton模式主要应用在一些特殊的类,在整个系统运行中,有且仅有一个实例的场景 二.Singleton模式分为单线程与多线程情况,当然多线程一样适应单线程 单线程:在这种情况下比较容易,因 ...

随机推荐

  1. 2018-2019-1 20165205 ch02 课下作业

    ch02 课下作业 2.96 代码 #include <stdio.h> #include <stdlib.h> typedef unsigned float_bits; fl ...

  2. leetcode98

    class Solution { public: vector<int> V; void postTree(TreeNode* node) { if (node != NULL) { if ...

  3. js对象拷贝遇到的坑

    问题:通过拷贝赋值后,所有的对象的name居然都是C test(){ let person = [{'name':'danny'}] let names = ['A','B','C'] let tem ...

  4. VULKAN学习资料

    1,中文开发教程:https://www.cnblogs.com/heitao/p/7193853.html

  5. JavaScript: For , For/in , For/of

    For: define: The for statement can customize how many times you want to execute code Grammar: for (c ...

  6. LAMP架构

    LAMP(linux,apache,mysql,php)是linux系统下常用的网站架构模型,用来运行PHP网站.(这得apache是httpd服务),这些服务可以安装同意主机上,也可以安装不同主机上 ...

  7. transform(转)

    转自:https://zhuanlan.zhihu.com/p/54356280

  8. int和string之间的转换

    #include<cstring> #include<algorithm> #include<stdio.h> #include<iostream> # ...

  9. 框架和内嵌框架--->frameset 和 iframe 的文档对象

    框架和内嵌框架分别用 HTMLFrameElemnt 和 HTMLIFrameElement 表示,它们在 DOM2 中有一个新属性----->contentDocument,是一个指针,表示框 ...

  10. 《CSAPP》页表、页命中、缺页

    页表 虚拟存储器系统需要某种方法来判定一个虚拟页是否在DRAM的某个地方. 这些功能由操作系统.存储器管理单元(MMU)中的地址翻译硬件和一个存放在物理存储器中的页表数据结构联合提供. 功能 将虚拟页 ...