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

网站架构设计师主要做什么全国疫情高峰感染进度

网站架构设计师主要做什么,全国疫情高峰感染进度,中学网站asp模板,做性视频网站有哪些内容前言 本篇文章继续探索通过继承实现单例模式的可行方案&#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.tj-hxxt.cn/news/125679.html

相关文章:

  • 德州做网站哪家好建站平台有哪些
  • 网站制作替我们购买域名热搜词排行榜
  • asp.net做网站深圳全网信息流推广公司
  • 唱片公司网站模板推广的软件
  • 哈尔滨市城乡建设委员会网站seo深圳优化
  • 中国工商信息注册网seo搜索引擎优化期末及答案
  • angularjs网站模板上海网络推广培训学校
  • 网站开发的就业西安网站维护公司
  • html自我介绍网页模板代码网站建设及推广优化
  • 适合团购报名的网站开发抖音关键词优化排名靠前
  • 企业网站网上推广的途径抖音代运营
  • 网站正在建设中 html源码自己手机怎么免费做网站
  • 网站建设怎么插入图片新网seo关键词优化教程
  • 网站关键词搜索如何排名靠前百度搜索关键词规则
  • 网站开发域名淘宝关键词优化推广排名
  • 网站群建设项目招标公告wordpress
  • 班级app网站建设公司企业网站制作需要多少钱
  • 广东营销型网站建设seo方法培训
  • 做信息网站怎么样广州网站建设费用
  • 正规营销型网站定制国产最好的a级suv
  • 网站建设公司那家好视频广告联盟平台
  • 直播网站怎么做的网站推广策划书模板
  • 用php做京东网站页面苏州做网站的专业公司
  • 搬瓦工快照恢复wordpressseo运营人士揭秘
  • 做网站ceo临沂网站建设公司哪家好
  • 做海报推荐网站今日新闻最新头条10条
  • 崇明专业做网站北京aso优化
  • 常德做网站专业公司网站推广怎样做
  • 网站模板带有sql后台下载宁波网站seo诊断工具
  • 运动服装商城网站建设品牌推广策略分析