·单例模式的特点:
1. 单例类只能有一个实例。
2. 单例类必须自己创建自己的唯一实例。
3. 单例类必须给所有其他对象提供这一实例。
·各种单例模式的特点:
·懒汉式是延时加载,在需要的时候才创建对象,避免内存浪费,但存在线程安全问题。
·饿汉式线程安全,类一加载就实例化对象,所以要提前占用系统资源。
·登记式单例模式克服了饿汉以及懒汉单例的不可继承问题,其子类在登记时才被创建,并且子类的实例化方式只能是饿汉式。
·各种单例模式的类图:
·饿汉式单例模式
·懒汉式单例模式:
·登记式单例模式:
· 各种单例模式的实现(Java):
1 package com.neu.core; 2 ?3 /** 4 ?* Only once instance of the class may be created during the execution of any 5 ?* given program. Instances of this class should be acquired through the 6 ?* getInstance() method. Notice that there are no public constructors for this 7 ?* class. 8 ?*/ 9 public class EagerSingleton {10 ????private static final EagerSingleton m_instance = new EagerSingleton();11 12 ????protected EagerSingleton() { }13 14 ????public static EagerSingleton getInstance() {15 ????????return m_instance;16 ????}17 }
=======================神奇的分割线==========================
1 package com.neu.core; 2 ?3 /** 4 ?* Only once instance of the class may be created during the execution of any 5 ?* given program. Instances of this class should be acquired through the 6 ?* getInstance() method. Notice that there are no public constructors for this 7 ?* class. 8 ?*/ 9 public class LazySingleton {10 ????private static LazySingleton m_instance = null;11 12 ????private LazySingleton() { }13 14 ????synchronized public static LazySingleton getInstance() {15 ????????if (m_instance == null) {16 ????????????m_instance = new LazySingleton();17 ????????}18 ????????return m_instance;19 ????}20 }
=======================神奇的分割线==========================
1 package com.neu.core; 2 ?3 import java.util.HashMap; 4 ?5 public class RegSingleton { 6 ????static private HashMap m_registry = new HashMap(); 7 ?8 ????static { 9 ????????RegSingleton x = new RegSingleton();10 ????????m_registry.put(x.getClass().getName(), x);11 ????}12 13 ????protected RegSingleton() {14 ????}15 16 ????static public RegSingleton getInstance(String name) {17 ????????if (name == null) {18 ????????????name = "com.neu.core.RegSingleton";19 ????????}20 21 ????????// System.out.println("From RegSingleton: requesting for " + name );22 23 ????????if (m_registry.get(name) == null) {24 ????????????try {25 ????????????????m_registry.put(name, Class.forName(name).newInstance());26 ????????????} catch (Exception e) {27 ????????????????e.printStackTrace();28 ????????????}29 ????????}30 ????????return (RegSingleton) (m_registry.get(name));31 ????}32 33 ????public String about() {34 ????????return "Hello, I am RegSingleton.";35 ????}36 37 }
1 package com.neu.core; 2 ?3 /** 4 ?* This class is a subclass of RegSingleton 5 ?*/ 6 public class RegSingletonChild extends RegSingleton { 7 ????public RegSingletonChild() { 8 ????} 9 10 ????static public RegSingletonChild getInstance() {11 ????????return (RegSingletonChild) RegSingleton.getInstance("com.javapatterns.singleton.demos.RegSingletonChild");12 ????}13 14 ????public String about() {15 ????????return "Hello, I am RegSingletonChild.";16 ????}17 18 }
[Js-设计模式]单例模式(饿汉,懒汉,登记式)
原文地址:https://www.cnblogs.com/jiasq/p/8524886.html