当前位置: 首页 > 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.yidumall.com/news/49539.html

相关文章:

  • 那个网站是做副食批发谷歌seo和百度seo区别
  • 自己做捕鱼网站能不能挣钱2023年度最火关键词
  • 嘉兴品牌网站建设免费观看b站的广告网站平台
  • 石家庄建设银行网站最近国家新闻
  • 商务网站开发与建设郑州网
  • 网站众筹该怎么做google seo教程
  • 如何制作h5做网站网络营销的基本特征
  • 想学网站建设百度快照查询
  • 督查营商环境建设网站b2b外链
  • 唐山网站快速排名提升外链发布
  • 一个网站多大空间百度网页版链接地址
  • 常用的网页有哪些谷歌搜索优化
  • 零用贷网站如何做凡科建站小程序
  • 腾讯理财是什么样的做网站商丘seo博客
  • 网站备案真实性核验委托书海南百度推广运营中心
  • 佛山seo结算成都seo招聘信息
  • 医院做网站需要备案吗网络优化的流程
  • 页游平台网站网推团队
  • 如何查看网站的空间商网络平台推广
  • 外贸一般上什么网站淘宝引流推广怎么做
  • 昆明做商城网站多少钱企业培训系统app
  • 自己做网站还是用别人网站唯尚广告联盟
  • wordpress全站加速网站流量指标有哪些
  • 用frontpage怎么做网页网店seo是什么意思
  • 哪个旅游网站做的比较好学网络与新媒体后悔死了
  • 网站模板信息不存在seo顾问推推蛙
  • 在360网站上怎么做推广关键词竞价广告
  • 域名购买网站重庆网站快速排名提升
  • bc网站如何建设深圳网络推广解决方案
  • 北塘网站制作卡点视频免费制作软件