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

公司做网站 分录谷歌seo代运营

公司做网站 分录,谷歌seo代运营,网站开发设计师岗位职责,小程序开发平台多少钱前言 本篇文章继续探索通过继承实现单例模式的可行方案&#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/57202.html

相关文章:

  • 网站附件做外链嘉兴关键词优化报价
  • 网站创建网站seo优化流程
  • 做地方门户网站如何做东莞网络推广托管
  • 阳信网站建设深圳防疫措施优化
  • 高端营销型网站建设小程序开发流程详细
  • wordpress wp contentseo公司系统
  • 山东莱芜疫情最新消息长沙seo搜索
  • 免费建立手机网站关键词搜索引擎又称为
  • 做网站需要许可证吗企业网站注册域名的步骤
  • 企业管理论文官网seo是什么意思
  • 网站不设置关键词描述企业seo排名费用报价
  • 百度能做网站建设吗网站入口
  • 南宁网站建设专家专门代写平台
  • 做网站 免费字体国内最新新闻事件今天
  • 陕西网站建设费用吉林seo外包
  • https网站开发如何配置东莞有哪些做推广的网站
  • 推广公司名字大全如何优化培训体系
  • 网站关键词搜索排名做小程序公司哪家好
  • 黄山建设厅官方网站深圳网站推广公司
  • 建设项目验收在哪个网站公示渠道策略的四种方式
  • 深圳信科做网站网络营销策划书2000字
  • 百度开户做网站2400佛山网站建设制作公司
  • 四川网站开发公司百度指数官方
  • 网站怎么做落款网址链接
  • 自己做的网站打开超慢江东seo做关键词优化
  • 站长工具seo综合查询columbu cat磁力最好用的搜索引擎
  • 99到家网站怎么做推广普通话手抄报内容50字
  • 延庆网站建设师肇庆网站推广排名
  • 大型在线网站建设网络营销案例分析论文
  • 昆明疫情最新情况今天前端seo优化