首页 > 其他分享 >Csharp: Singleton Patterns

Csharp: Singleton Patterns

时间:2022-09-18 16:58:21浏览次数:64  
标签:Singleton spooler private Patterns Csharp Spooler public

 

    /// <summary>
    /// Summary description for Spooler.
    /// geovindu,Geovin Du,涂聚文
    /// 单例 模式(Singleton Patterns)
    /// </summary>
    public class Spooler
    {
        /// <summary>
        /// 
        /// </summary>
        private static bool instance_flag = false;
        /// <summary>
        /// 
        /// </summary>
        private Spooler()
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static Spooler getSpooler()
        {
            if (!instance_flag)
                return new Spooler();
            else
                return null;
        }

    }

  

    /// <summary>
    /// Summary description for SingletonException.
    /// geovindu,Geovin Du,涂聚文
    /// 单例 模式(Singleton Patterns)
    /// </summary>
    public class SingletonException : Exception
    {
        /// <summary>
        /// new exception type for singleton classes
        /// </summary>
        /// <param name="s"></param>
        public SingletonException(string s)
            : base(s)
        {
        }
    }

  

调用测试:

 /// <summary>
    /// geovindu,Geovin Du,涂聚文
    /// 单例 模式(Singleton Patterns)
    /// </summary>
    public partial class SingletonPatternsForm : Form
    {

        /// <summary>
        /// 
        /// </summary>
        public SingletonPatternsForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SingletonPatternsForm_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Spooler sp1 = Spooler.getSpooler();
            if (sp1 != null)
            {
                this.textBox1.Text = "Got 1 spooler";
                Console.WriteLine("Got 1 spooler");
            }
            Spooler sp2 = Spooler.getSpooler();
            if (sp2 == null)
            {
                this.textBox2.Text = "Can\'t get spooler";
                Console.WriteLine("Can\'t get spooler");
            }
            else
            {
                
                this.textBox2.Text = "Got 2 spooler";
            }
            //fails at compile time 不可访问,受保护级别
           // Spooler sp3 = new Spooler();


        }
    }

  

标签:Singleton,spooler,private,Patterns,Csharp,Spooler,public
From: https://www.cnblogs.com/geovindu/p/16705187.html

相关文章

  • Csharp: Prototype Patterns
     ///<summary>///SummarydescriptionforSwimmer///geovindu,GeovinDu,涂聚文///原型模式(PrototypePatterns)///</summary>public......
  • dependency-injection-principles-practices-and-patterns 阅读记录
    最近断断续续看了一下dependency-injection-principles-practices-and-patterns这本书,随便记录了一下思维导图,结合目前的经验还是感觉到收获不少。......
  • Csharp: Builder Patterns
     ///<summary>///SummarydescriptionforEquities.///geovindu,GeovinDu,涂聚文///生成器(建造)模式(BuilderPatterns)///</summary>......
  • Singleton_单例模式:
    Singleton_单例模式:常规实现: publicclassSingleton{ privateSingleton(){ } privatefinalstaticSingletonINSTANCE=newSingleton(); publicstaticSingl......
  • 用 CSharpCompilation 进行动态编译
    项目里需要用到动态编译。网上一大片的介绍C#动态编译的,是CodeDomProvider,这个东西确实好用。但是说好了支持.net6.0 的,但运行时却说不支持当前平台。骗子!网上找的......
  • [CSharpTips]C# 将DataTable转换为类
    将DataTable转换为类众所周知,有时候我们需要将sql查询返回的DataTable转换为类。最开始是使用循环一个个给类的属性赋值,但是这样效率低并且无法复用。后来了解到利用Data......
  • [CSharpTips]C# 读写INI文件
    读写.ini文件工具可以直接使用usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Text;names......
  • [Javascript] Singleton Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/design-patterns/singleton-patternWiththeSingletonPattern,werestricttheinstantiationofcertainc......
  • csharp: Zip and Unzip files using GZipStream
      usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.......