学习设计模式,一直没有机会写一个单例模式。

今天在控制台应用程序,写个简单的例子,Hi与Hello。

单例模式(C#)

 

 public sealed class At
    {
        private static At instance = null;
        public static At Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new At();
                }
                return instance;
            }
        }

        public void Hello()
        {
            Console.WriteLine("Hello");
        }

        public void Hi()
        {
            Console.WriteLine("Hi");
        }
    }
Source Code

相关文章: