Singleton Summary
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.
- Eager initialization
- Static block initialization
- Lazy Initialization
- Thread Safe Singleton
- Bill Pugh Singleton Implementation
- Using Reflection to destroy Singleton Pattern
- Enum Singleton
- 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的更多相关文章
- 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 ...
- C#面向对象设计模式纵横谈——2.Singleton 单件(创建型模式)
一:模式分类 从目的来看: 创建型(Creational)模式:负责对象创建. 结构型(Structural)模式:处理类与对象间的组合. 行为型(Behavioral)模式:类与对象交互中的职责分配 ...
- Net设计模式实例之单例模式( Singleton Pattern)
一.单例模式简介(Brief Introduction) 单例模式(Singleton Pattern),保证一个类只有一个实例,并提供一个访问它的全局访问点.单例模式因为Singleton封装它的唯 ...
- 【转】单例模式(Singleton)
首先来明确一个问题,那就是在某些情况下,有些对象,我们只需要一个就可以了, 比如,一台计算机上可以连好几个打印机,但是这个计算机上的打印程序只能有一个, 这里就可以通过单例模式来避免两个打印作业同时输 ...
- 设计模式(一)单例模式(Singleton Pattern)
一.引言 最近在设计模式的一些内容,主要的参考书籍是<Head First 设计模式>,同时在学习过程中也查看了很多博客园中关于设计模式的一些文章的,在这里记录下我的一些学习笔记,一是为了 ...
- 跨应用程序域(AppDomain)的单例(Singleton)实现
转载自: 跨应用程序域(AppDomain)的单例(Singleton)实现 - CorePlex代码库 - CorePlex官方网站,Visual Studio插件,代码大全,代码仓库,代码整理,分 ...
- 设计模式:单例模式(Singleton)
定义:确保一个类仅有一个实例,并提供一个访问它的全局访问点. 优点:在内存中只有一个对象,节省了内存空间 示例: Singleton.cs 写法一:非线程安全 public class Singlet ...
- 单例模式(Singleton)的6种实现
1.1.1 摘要 在我们日常的工作中经常需要在应用程序中保持一个唯一的实例,如:IO处理,数据库操作等,由于这些对象都要占用重要的系统资源,所以我们必须限制这些实例的创建或始终使用一个公用的实例,这就 ...
- 1、Singleton 单件(创建模式)
一.Singleton模式主要应用在一些特殊的类,在整个系统运行中,有且仅有一个实例的场景 二.Singleton模式分为单线程与多线程情况,当然多线程一样适应单线程 单线程:在这种情况下比较容易,因 ...
随机推荐
- Redis-Sentinel 数据源配置
1.redis配置文件 : redis.properties # Redis settings #sentinel_node_1 redis.sentinel1.host=192.168.0.1 re ...
- 【学习】pandas 基础介绍说明 【pandas】
本文来源于<利用python进行数据分析>中文版,大家有兴趣可以看原版,入门的东西得脚踏实地哈 1.pandas 数据结构介绍 首先熟悉它的两个主要数据结构,Series 和 DataFr ...
- leetcode56
public class Solution { public IList<Interval> Merge(IList<Interval> intervals) { var le ...
- 【Noip模拟 20160929】选数
题目描述 现在有一排共N个数,你需要从中选出恰好K个.选出K个数后,计算它们两两差值的绝对值的最小值S.你需要确定选出哪K个,才能最大化这个S. 输入数据 输入第一行两个正整数N.K,含义如上. 输入 ...
- [C语言]进阶|链表
--------------------------------------------------------------------------------------- 可变数组: array. ...
- TXLSReadWriteII5 单元格读写
unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- keepalive高可用
Keepalived软件起初是专门为LVS负载均衡软件设计的,用来管理并监控LVS集群系统中各个服务节点的状态,后来又加入了可以实现高可用的VRRP功能.因此,Keepalived除了能够管理LVS软 ...
- 最小费用最大流 HDU1533
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1533 #include<bits/stdc++.h> #define fi first ...
- 禁止 pdf 下载、打印的方法
https://blog.csdn.net/For_GG/article/details/78616063 https://blog.csdn.net/xiangcns/article/details ...
- kubernetes 基础
官网 kubernetes.io 有中文 中文网站 http://docs.kubernetes.org.cn kubectl 详细情况 https://kubernetes.io/docs/ref ...