zoukankan      html  css  js  c++  java
  • 设计模式学习总结5 创建型5 Singleton单例模式

    作用:
    Singleton单例模式目的在于确保一个类只有唯一的一个实例,并且这个唯一实例只有一个全局访问点。它确保类被实例一次,所有这个类的请求都指向这个唯一的实例。另外,这个对象不是在需要的时候才被创建。在Singleton单例模式中,是由单例类来保证这种约束的,而不是客户端通过其他方法实现类的唯一实例。
    Role
    The purpose of the Singleton pattern is to ensure that there is only one instance of a
    class, and that there is a global access point to that object. The pattern ensures that
    the class is instantiated only once and that all requests are directed to that one and
    only object. Moreover, the object should not be created until it is actually needed. In
    the Singleton pattern, it is the class itself that is responsible for ensuring this con-
    straint, not the clients of the class.

    设计:

    UniqueSingletonInstance,私有静态的Singleton类型成员
    Singleton(),私有构造函数
    GetInstance(),公共获取实例函数
    实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Singleton
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                sigCls sigObj 
    = sigCls.GetInstance();
                Console.WriteLine(sigObj.strData);
                Console.WriteLine(sigObj.GetHashCode().ToString());
               
                Console.ReadLine();
                Console.WriteLine(sigObj.strData);
            }
        }
        
    public class sigCls
        {
            
    private static sigCls sigInstance;
            
    private sigCls()
            {
                strData 
    = "Sig";
            }

            
    public string strData
            {
                
    get;
                
    set;
            }
            
    public static sigCls GetInstance()
            {
                
    if (sigInstance == null)
                    sigInstance 
    = new sigCls();
                
    return sigInstance;
            }

        }
    }

    使用场景:

    1、确保类仅存在唯一的实例
    2、控制访问这个实例在系统中非常关键
    3、在类实例以后的会多次使用这个实例
    4、由实例化的类来控制单例,而不是由其他机制来控制。

    Use the Singleton pattern when …
    •  You need to ensure there is only one instance of a class.
    •  Controlled access to that instance is essential.
    •  You might need more than one instance at a later stage.
    •  The control should be localized in the instantiated class, not in some other mechanism.
    总结:

    有一些类在系统中只存在一个实例才能确保他们的逻辑正确性以及良好的效率。单件模式的使用意图就是:保证一个类仅有一个实例,并提供一个该实例全局的访问点。单例类使用私有的构造函数。Singleton t1 = new Singleton(),编译时告诉我Singleton()不可访问,构造函数是私有的。单线程下的单件模式有几点要注意:
    1、 构造器私有化(如果要此类被继承,可以用protected声明构造器)
    2、 不要支持IClinieable接口,因为会导致多个对象实例的出现
    3、 不能支持序列化
    4、 单件模式只考虑了对象创建的管理,没有考虑对象的销毁管理(创建自己的对象,销毁的事交给垃圾回收器吧)
    5、 不能应对多线程环境,因为会导致多个对象实例的出现
    在多线程下如何实现呢?代码如下:

     

    代码
    class SingletonMuli//多线程Singleton模式
    {
            
    private static volatile SingletonMuli _instance;    //volatile是为了让编译器对此代码编译后的位置不进行调整
            private SingletonMuli(){}
            
    private static object lockHelper = new object();    //辅助器,不参与对象构建
            public static SingletonMuli f_Instance
            {
                
    get
                {
                    
    if(_instance == null)
                    {
                        
    lock(lockHelper)
                        {
                            
    if(_instance == null)       //双检查
                            {
                                _instance 
    = new SingletonMuli();
                            }
                        }
                    }
                    
    return _instance;
                }
            }
        }






     

  • 相关阅读:
    谢惠民,恽自求,易法槐,钱定边编数学分析习题课讲义16.2.3练习题参考解答[来自陶哲轩小弟]
    高考题(可作为试讲资料)
    美国数学教父拒绝10亿美元 免费分享教学视频
    梁启超死亡真相:主刀医生错把健康的肾切除
    [家里蹲大学数学杂志]第418期南开大学2013年实变函数期末考试试题参考解答
    陆启铿同志去世
    [电视剧]养父的花样年华
    [电视剧]错爱一生
    [家里蹲大学数学杂志]第413期插值不等式
    Oracle的 EXEC SQL CONTEXT学习
  • 原文地址:https://www.cnblogs.com/utopia/p/1676070.html
Copyright © 2011-2022 走看看