码迷,mamicode.com
首页 > Web开发 > 详细

[Js-设计模式]单例模式(饿汉,懒汉,登记式)

时间:2018-03-07 21:52:20      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:map   private   gis   ati   for   懒汉式单例模式   util   ext   stat   

·单例模式的特点:

  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-设计模式]单例模式(饿汉,懒汉,登记式)

标签:map   private   gis   ati   for   懒汉式单例模式   util   ext   stat   

原文地址:https://www.cnblogs.com/jiasq/p/8524886.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!