1:饿汉
public class SingletonExample {
private static SingletonExample _instance = new SingletonExample();
public static SingletonExample getInstance() {
return _instance;
}
private SingletonExample() {
}
}
2:懒汉
public class SingletonExample {
private static SingletonExample _instance = null;
public static synchronized SingletonExample getInstance() {
if(_instance == null){
_instance = new SingletonExample();
}
return _instance;
}
private SingletonExample() {
}
}
3:懒汉,不安全
public class SingletonExample {
private static SingletonExample _instance = null;
public static SingletonExample getInstance() {
if(_instance == null){
synchronized (SingletonExample.class){
if(_instance == null){
_instance = new SingletonExample();
}
}
}
return _instance;
}
private SingletonExample() {
}
}
4:懒汉,内部类
public class SingletonExample {
private static class SingletonExampleHolder{
private final static SingletonExample _instance = new SingletonExample();
}
public static SingletonExample getInstance() {
return SingletonExampleHolder._instance;
}
private SingletonExample() {
}
}
5:登记式,可继承
public class SingletonExample {
static private HashMap<String, Object> singletonMap = new HashMap<String, Object>();
static {
SingletonExample x = new SingletonExample();
singletonMap.put(x.getClass().getName(), x);
}
protected SingletonExample() {
}
public static SingletonExample getInstance(String name) {
if (name == null) {
name = "*.*.SingletonExample";
}
if (singletonMap.get(name) == null) {
try {
singletonMap.put(name, Class.forName(name).newInstance());
} catch (ClassNotFoundException cnf) {
System.out.println("Couldn't find class " + name);
} catch (InstantiationException ie) {
System.out.println("Couldn't instantiate an object of type "+ name);
} catch (IllegalAccessException ia) {
System.out.println("Couldn't access class " + name);
}
}
return (SingletonExample) (singletonMap.get(name));
}
}
class SingletonChild extends SingletonExample{
public SingletonChild(){}
public static SingletonChild getInstance(){
return (SingletonChild)SingletonExample.getInstance("*.*.SingletonChild");
}
}
分享到:
相关推荐
详细讲解了Java单例模式的几种实现方式,并有详细的示例配合讲解.
主要讲了单例模式的几种方法,懒汉模式、饿汉模式、静态内部类模式。着重讲解了懒汉模式下怎么实现线程安全。饿汉模式和静态内部类模式如何设置能够避免使用反射方法获取多个实列,以及实现了序列化的类如何避免创建...
自己总结的6中单例模式的写法,也有测试类,可以试验下,自己稍微修改一下后,验证安全性,纯粹为学习,建议可提
单例模式是一种最简单的创建型设计模式,主要用于对系统资源的管理与控制,在软件开发...文中通过内容管理系统项目,深入剖析了几种常用的单例类,包括饿汉式单例类、懒汉式单例类的应用,优化设计了JAVA双重检查单例模式。
二、单例模式的几种形式 1. 饿汉式单例 public class Singleton { private static final Singleton singleton= new Singleton(); private Singleton(){ } public Singleton getSingleton(){ return sin
主要介绍了Java单例模式实现的几种方式的相关资料,需要的朋友可以参考下
单例模式,完整介绍单例模式的几种创建方式 以及对比优缺点,引用spring 源码简单分析 框架如何保证单例
主要为大家详细介绍了C++和java设计模式之单例模式的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在java中,单例有很多种写法,面试时,手写代码环节,除了写算法题,有时候也会让手写单例模式,这里记录一下单例的几种写法和优缺点。需要的朋友可以参考下
单例模式,顾名思义就是只有一个实例,并且她自己负责创建自己的对象,这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。下面我们来看下有哪几种实现方式吧。
主要介绍了Java 单例模式的实现的相关资料,并附简单实例代码,需要的朋友可以参考下
3.源代码的package分类非常清楚,模式类型->模式名称->模式种类->示例->角色名称,让您一下就看清楚共几种设计模式,具体的一种设计模式又有几种细分,每种里面有哪几个角色,以及客户端调用如何调用; 4.注释非常全...
观察者模式 Observer:Swing中的事件模型 工厂模式 Factory:在JDK中遍地都是,比如JDBC、JNDI等,是学习Spring的基础 命令模式 Command:Struts框架的基石 单例模式 Singleton:最简单的设计模式,大量...
以JAVA为例,汇总了十几种常用的设计模式,包括了:单例模式、工厂模式、建造者模式、适配器模式、装饰器模式、外观模式、命令模式、观察者模式、状态模式、策略模式、模板方法模式等。仅供学习使用。 相关文章请看...
说在最前面:本篇文章不是讲单例模式有哪几种创建方式以及各自的利弊,也不说什么情况下单例模式失效以及内存泄漏等类似面试题,只说个人在应用中想要总结的一个小问题,如果冲着上面说的内容来的,那可以继续找其他...
本系统用以下几种设计模式单例模式,策略模式,状态模式,代理模式,组合模式。
本系列文章来总结一下设计模式在PHP中的应用,这是第一篇创建型模式之单例模式。 一、设计模式简介 首先我们来认识一下什么是设计模式: 设计模式是一套被反复使用、容易被他人理解的、可靠的代码设计经验的总结。 ...
在本系统中,代理商类、可口可乐公司类和百事可乐公司类都使用到了代理模式,而且代理商类使用了懒汉式来实现单例模式,可口可乐公司类使用了饿汉式实现单例模式,而百事可乐类使用了双重检查锁来实现单例模式。...
一般单例都是五种写法:懒汉,饿汉,双重校验锁,静态内部类和枚举。本文整理了几种常见的单例写法,下面跟着小编一起来看下吧