:.RUN.:

lighting storm

关于,Singleton。

// Singleton<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

// Intent: "Ensure a class only has one instance, and provide a global

// point of access to it".

 

// For further information, read "Design Patterns", p127, Gamma et al.,

// Addison-Wesley, ISBN:0-201-63361-2

 

/* Notes:

 * If it makes sense to have only a single instance of a class (a so-called

 * singleton), then it makes sense to enforce this (to elimintate potential

 * errors, etc).

 *

 * A class based on the singleton design pattern protects its constructor,

 * so that only the class itself (e.g. in a static method) may instantiate itself.

 * It exposes an Instance method which allows client code to retrieve the

 * current instance, and if it does not exist to instantiate it. 

 */

 

namespace Singleton_DesignPattern

{

    using System;

 

    class Singleton

    {

        private static Singleton _instance;

 

        public static Singleton Instance()

        {

            if (_instance == null)

                _instance = new Singleton();

            return _instance;

        }

        protected Singleton() { }

 

        // Just to prove only a single instance exists

        private int x = 0;

        public void SetX(int newVal) { x = newVal; }

        public int GetX() { return x; }

    }

 

    /// <summary>

    ///    Summary description for Client.

    /// </summary>

    public class Client

    {

        public static int <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main(string[] args)

        {

            int val;

            // can't call new, because constructor is protected

            Singleton FirstSingleton = Singleton.Instance();

            Singleton SecondSingleton = Singleton.Instance();

 

            // Now we have two variables, but both should refer to the same object

            // Let's prove this, by setting a value using one variable, and

            // (hopefully!) retrieving the same value using the second variable

            FirstSingleton.SetX(4);

            Console.WriteLine("Using first variable for singleton, set x to 4");

 

            val = SecondSingleton.GetX();

            Console.WriteLine("Using second variable for singleton, value retrieved = {0}", val);

            return 0;

        }

    }

}

 

阅读更多
个人分类: .NET
想对作者说点什么? 我来说一句

多线程下的单例模式优化

2018年04月04日 936B 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭