《模式——工程化实现及扩展》(设计模式C# 版)《原型模式 Prototype》——“自我检验"
自我检验
假设由你完成一个企业申报数据序列化的程序,企业信息实体需要按照“计算机信息系统集成资质”1级的要求将满足申报条件的信息序列化。请结合本章介绍的自定义深层复制方式,实现原型对象,并通过单元测试验证。
为了便于示例,对“计算机信息系统集成资质”1级的要求节选如下:
1、 近三年内完成至少两项3000万元以上系统集成项目
2、 具有计算机信息系统集成项目经理人数不少于25名,其中高级项目经理人数不少于8名
示例并不对企业整体是否满足申报要求进行审核,仅对满足申报要求的类成员进行筛选。示例涉及的数据结构如下,请改造Enterprise类为原型模式类型:
[Serializable]
class Project
{
/// <summary>
/// 结项年份
/// </summary>
public int EndYear { get; set; }
/// <summary>
/// 项目资金规模(万元)
/// </summary>
public double Scale { get; set; }
}
[Flags]
enum QualificationOptions
{
Assisstant, // 助理级
Professional, // 专家级
Senior, // 高级、资深级
Principal // 主任、主管级
}
[Serializable]
class Certification : IComparable<Certification>
{
public string Name { get; set; }
public QualificationOptions Qualification { get; set; }
public int CompareTo(Certification other)
{
if(other == null) throw new ArgumentNullException("other");
if(!string.Equals(other.Name, Name)) throw new NotSupportedException();
if(Qualification == other.Qualification) return 0;
return Qualification > other.Qualification ? 1 : -1;
}
}
[Serializable]
class Employee
{
public string Name { get; set; }
public Certification[] Certificates { get; set; }
}
[Serializable]
class Enterprise : ISerializable
{
public string Name { get; set; }
public Project[] Projects { get; set; }
public Employee[] Staff { get; set; }
}
class Project
{
/// <summary>
/// 结项年份
/// </summary>
public int EndYear { get; set; }
/// <summary>
/// 项目资金规模(万元)
/// </summary>
public double Scale { get; set; }
}
[Flags]
enum QualificationOptions
{
Assisstant, // 助理级
Professional, // 专家级
Senior, // 高级、资深级
Principal // 主任、主管级
}
[Serializable]
class Certification : IComparable<Certification>
{
public string Name { get; set; }
public QualificationOptions Qualification { get; set; }
public int CompareTo(Certification other)
{
if(other == null) throw new ArgumentNullException("other");
if(!string.Equals(other.Name, Name)) throw new NotSupportedException();
if(Qualification == other.Qualification) return 0;
return Qualification > other.Qualification ? 1 : -1;
}
}
[Serializable]
class Employee
{
public string Name { get; set; }
public Certification[] Certificates { get; set; }
}
[Serializable]
class Enterprise : ISerializable
{
public string Name { get; set; }
public Project[] Projects { get; set; }
public Employee[] Staff { get; set; }
}
贸易电子化,技术全球化
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?