C# 原型模式

 

 

class Program
{
static void Main(string[] args)
{
#region v0.1
//ConcretePrototypel pl = new ConcretePrototypel("1");
//ConcretePrototypel c1 = (ConcretePrototypel)pl.Clone();
//Console.WriteLine("Cloned:{0}", c1.Id);
//Console.Read();
#endregion

#region v0.2 使用win接口
//Resume a = new Resume("大鸟");
//a.SerPersonalInfo("男","29");
//a.SetWorkExperience("1998-2000","xx公司");

//Resume b = (Resume)a.Clone();
//b.SetWorkExperience("1998-2006","yy公司");

//Resume c = (Resume)a.Clone();
//c.SerPersonalInfo("男","24");

//a.Display();
//b.Display();
//c.Display();
//Console.Read();
#endregion

#region 浅复制: 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象
Resume a = new Resume("大鸟");
a.SerPersonalInfo("男","29");
a.SetWorkExperience("1998-2000","xx公司");

Resume b = (Resume)a.Clone();
b.SetWorkExperience("1998-2006","yy公司");

Resume c = (Resume)a.Clone();
c.SetWorkExperience("1998-2003","zz企业");
a.Display();
b.Display();
c.Display();
Console.Read();
#endregion

}
}

 

/// <summary>
/// 1原型类
/// </summary>
abstract class Prototype
{
private string id;
public Prototype(string id)
{
this.id = id;
}

public string Id
{
get{ return id; }
}

public abstract Prototype Clone();
}

 

/// <summary>
/// 2具体原型类
/// </summary>
class ConcretePrototypel : Prototype
{
public ConcretePrototypel(string id):base(id)
{
}

public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}

 

/// <summary>
/// 3工作经历,浅复制
/// </summary>
class WorkExperience:ICloneable
{
private string workDate;
public string WorkDate
{
get{return workDate;}
set{workDate = value;}
}

private string company;
public string Company
{
get { return company; }
set { company = value;}
}

#region 引入ICloneable接口,做深复制
public object Clone()
{
return (Object)this.MemberwiseClone();
}
#endregion

}

 

/// <summary>
/// 4简历:使用win接口
/// </summary>
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
//private string timeArea;
//private string company;

private WorkExperience work;

public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}

#region 深复制
public Resume(WorkExperience work)
{
this.work = (WorkExperience)work.Clone();
}
#endregion

//设置个人信息
public void SerPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}

//显示工作信息
//public void SetWorkExperience(string timeArea, string company)
//{
// this.timeArea = timeArea;
// this.company = company;
//}

//显示工作信息
public void SetWorkExperience(string workDate, string company)
{
work.WorkDate = workDate;
work.Company = company;
}

//public void Display()
//{
// Console.WriteLine("{0},{1},{2}",name,sex,age);
// Console.WriteLine("工作经历:{0},{1}",timeArea,company);
//}

public void Display()
{
Console.WriteLine("{0},{1},{2}", name, sex, age);
Console.WriteLine("工作经历:{0},{1}", work.WorkDate,work.Company);
}

#region 浅复制
//public object Clone()
//{
// return (Object)this.MemberwiseClone();
//}
#endregion

#region 深复制
public object Clone()
{
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
#endregion

}

 

posted @ 2022-04-02 10:18  KevinSteven  阅读(34)  评论(0编辑  收藏  举报