当前位置: 首页 > news >正文

江都城乡建设局网站网页建设软件

江都城乡建设局网站,网页建设软件,视频怎么下载,阿里云虚拟主机网站吗前言 本篇文章继续探索通过继承实现单例模式的可行方案&#xff0c;这次的方案将采用反射机制隐式创建派生类实例&#xff0c;示例代码为C#。 代码 v1.0 using System.Reflection;/// <summary> /// 单例模式基类 /// </summary> /// <typeparam name"T&…

前言

本篇文章继续探索通过继承实现单例模式的可行方案,这次的方案将采用反射机制隐式创建派生类实例,示例代码为C#。

代码

v1.0

using System.Reflection;/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class Singleton<T>
where T : class
{public static T instance => _instance.Value;static readonly Lazy<T> _instance = new Lazy<T>(Create);#pragma warning disable CS8603static T Create(){return Activator.CreateInstance(typeof(T),BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,null, null, null) as T;}
#pragma warning restore CS8603
}

测试

单例模式基类

/// <summary>
/// 单例模式基类
/// </summary>
/// <typeparam name="T">单例类型</typeparam>
public abstract class SingletonWithTest<T>
where T : class
{public static T instance => _instance.Value;static readonly Lazy<T> _instance = new Lazy<T>(Create);#pragma warning disable CS8603static T Create(){return Activator.CreateInstance(typeof(T),BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,null, null, null) as T;}
#pragma warning restore CS8603
}

派生类

public class TestA : SingletonWithTest<TestA>
{public readonly string key;TestA() { key = "Default A"; }
}public class TestB : SingletonWithTest<TestB>
{public readonly string key;public TestB() { key = "Default B"; }
}public class TestC : SingletonWithTest<TestC>
{public readonly string key;public TestC(string key) { this.key = key; }
}public class TestD : SingletonWithTest<TestD>
{public readonly string key;public TestD() { key = "Default D"; }public TestD(string key) { this.key = key; }
}public class TestE : SingletonWithTest<TestE>
{public readonly string key;TestE() { key = "Default E"; }public TestE(string key) { this.key = key; }
}public class TestF : SingletonWithTest<TestF>
{public readonly string key = "Default F";
}

测试代码

// ********************************* 线程安全测试:通过 *********************************Thread t1, t2, t3, t4, t5, t6;// 打印同一单例的 HashCode 测试:通过
// t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
// t2 = new Thread(() => Console.WriteLine("Thread2:" + TestA.instance.GetHashCode()));
// t3 = new Thread(() => Console.WriteLine("Thread3:" + TestA.instance.GetHashCode()));
// t4 = new Thread(() => Console.WriteLine("Thread4:" + TestA.instance.GetHashCode()));
// t5 = new Thread(() => Console.WriteLine("Thread5:" + TestA.instance.GetHashCode()));
// t6 = new Thread(() => Console.WriteLine("Thread6:" + TestA.instance.GetHashCode()));// 同时使用不同单例的测试:通过
t1 = new Thread(() => Console.WriteLine("Thread1:" + TestA.instance.GetHashCode()));
t2 = new Thread(() => Console.WriteLine("Thread2:" + TestB.instance.GetHashCode()));
t3 = new Thread(() => Console.WriteLine("Thread3:" + TestC.instance.GetHashCode())); // 没有无参构造函数,触发异常
t4 = new Thread(() => Console.WriteLine("Thread4:" + TestD.instance.GetHashCode()));
t5 = new Thread(() => Console.WriteLine("Thread5:" + TestE.instance.GetHashCode()));
t6 = new Thread(() => Console.WriteLine("Thread6:" + TestF.instance.GetHashCode()));t1.Start();
t2.Start();
t3.Start();
t4.Start();
t5.Start();
t6.Start();t1.Join();
t2.Join();
t3.Join();
t4.Join();
t5.Join();
t6.Join();

优缺点分析

优点

1.继承实现单例模式;

2.按需加载,延迟初始化;

3.线程安全;

4.可以通过单例基类规范统一标准;

5.派生类的无参构造函数用于初始化;

6.将派生类实例创建权限交由派生类本身决定,通常,为遵循单例模式的原则应向外部关闭通过new关键字显式调用构造函数的权限,但这对派生类而言并非硬性要求;

缺点

1.反射开销;

2.派生类必须具备无参构造函数,且无参构造函数需要向单例基类提供可进行反射访问的权限;

版本改进

......

系列文章

继承实现单例模式的探索(一)

如果这篇文章对你有帮助,请给作者点个赞吧!

http://www.yayakq.cn/news/25277/

相关文章:

  • 白之家 低成本做网站网站如可引导客户
  • 墨刀做网站单位网站建设 管理制度
  • 焦作网站开发公司小程序开发教程知乎
  • 网站建设商标属于哪个类别机械制造设备类企业网站织梦模板
  • 做滤芯的网站群晖 wordpress 配置文件
  • asp.net mvc 做网站长治网站制作厂家
  • 做微商在哪个网站打广告好柳州建设网栗园新居
  • 手机建公司网站网站建设属于什么类的采购
  • 国外优秀的平面设计网站手机商城app开发公司
  • 网站主持人制作方法wordpress 图片收录
  • 怎样做网站后台优化快速排名新
  • 90设计网站可以商用吗濮阳网络电视台
  • 久久建筑网论坛关键词排名优化提升培训
  • wordpress登陆logo湛江市seo网站设计哪里好
  • 京东pc网站用什么做的dnf怎么做发卡网站
  • 响应式网站展示型网站建设制作模板
  • 遵义制作公司网站的公司十大跨境电商公司排名
  • 在线公司网站查询wordpress文章保存目录
  • 手机网站免费优化湖南外发加工网
  • 门户网站 特点电商网站代码设计
  • 辽宁省建设安装集团网站微信短网址生成
  • 网站应如何设计协会工作方案网站建设困难
  • 自己建网站百度到吗做网站需要用什么开发软件
  • 当涂 微网站开发公司正规游戏代理平台
  • 谁给推荐一个免费的好网站织梦网站建设交流群
  • 湛江网站关键词优化前端工作好找吗
  • 唐山做企业网站公司html企业网站模板
  • 学校网站制作价格自己做网站百度会收录
  • 2016年网站设计风格青岛企业建站系统
  • 山东省示范校建设网站百度一下首页官网百度