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.
Design:
Implementation:
public sealed class Singleton
{
// Private constructor. private Singleton()
{
Console.WriteLine("One instance is created.");
}
// private object instantiated with private constructor. private static readonly Singleton instance = new Singleton();
// Public static property to get the object. public static Singleton UniqueInstance
{
get
{
return instance;
}
}
}
class Program
{
static void Main(string[] args)
{
Singleton s0= Singleton.UniqueInstance;
Singleton s1= Singleton.UniqueInstance;
Singleton s2= Singleton.UniqueInstance;
}
}
当前题目:(DesignPattern)Singleton.-创新互联
本文链接:https://www.cdcxhl.com/article46/coephg.html
成都网站建设公司_创新互联,为您提供网站内链、品牌网站设计、自适应网站、外贸建站、企业建站、网站改版
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联