原型模式 - Prototype

来源:互联网 发布:淘宝卖家去哪找淘客? 编辑:程序博客网 时间:2024/05/16 16:09

原型模式(Prototype),用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象。

原型模式结构图:

原型模式其实就是从一个对象再创建另外一个可定制的对象,而且不需要知道任何创建的细节。我们来看看基本的原型模式代码。
原型类
abstract class Prototype
{
 private string id;
 
 public Prototype(string id)
 {
  this.id = id;
 }

 public string Id
 {
  get { return id; }
 }
 
 public abstract Prototype Clone();
}
具体原型类
class ConcretePrototype1 : Prototype
{
 public ConcretePrototype1(string id) : base(id)
 {

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

---客户端代码---
static void Main(string[] args)
{
 ConcretePrototype1 p1 = new ConcretePrototype1("I");
 //克隆类 ConcretePrototype1的对象p1就能得到新的实例c1
 ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
 Console.WriteLine("Cloned {0}",c1.Id);

 Console.Read();
}

对于.net而言,那个原型抽象类Prototype是用不着的,因为克隆实在是太常用了,所以.net在System命名这间中提供了ICloneable接口,其中就是唯一的一个方法Clone(),这样你就只需要实现这个接口就可完成原型模式了。

简历复印的实现
class Resume : IConeable
{
 private string name;
 private string sex;
 private string age;
 private string timeArea;
 private string company;

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

 //设置个人信息
 public void SetPersonalInfo(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 Display()
 {
  Console.WriteLine("{0},{1},{2}",name,sex,age};
  Console.WriteLine("工作经历:{0} {1}",timeArea,company);
 }

 public Object Clone
 {
  return (Object)this.MemberwiseClone();
 }
}

---客户端调用代码---
static void Main(string[] args)
{
 Resume a = new Resume("大鸟");
 a.SetPersonalInfo("男","29");
 a.setWorkExperience("1988-2000","XX 公司");

 Resume b =(Resume)a.Clone();
 b.setWorkExperience("1988-2006","CC公司");

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

 a.Display();
 b.Display();
 c.Display();
}

一般在初始化信息不发生变化的情况下,克隆是最好的办法,即隐藏了对象创建的细节,又对性能是大大的提高。

浅复制与深复制
MemberwiseClone()方法是这样的,如果字段是值类型的,则对该字段逐位复制,如果字段是引用类型,则复制引用但不复制引用的对象山因此,原始对象及其复本引用同一对象。
深复制把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。

简历的深复制实现
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; }
 }
 public Object Clone()
 {
  return (Object)this.MemberwiseClone();
 }
}

class Resume : IClone
{
 private string name;
 private string sex;
 private string age;
 private WorkExperience work;

 public Resume(string name)
 {
  this.name = name;
  work = new WorkExperience();
 }
 private Resume(WorkExperience work)
 {
  this.work = (WorkExperience)work.Clone();
 }
 //设置个人信息
 public void SetPersonalInfo(string sex,string age)
 {
  this.sex = sex;
  this.age = age;
 }
 //设置工作经历
 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}",work.workDate,work.company);
 }

 public Object Clone
 {
  //调用私有的构造方法,让”工作经历“克隆完成,然后再给这个简历对象的相关字段赋值,最终返回一个深复制的的简历对象。
  Resume obj = new Resume(this.work);
  obj.name = this.name;
  obj.sex = this.sex;
  obj.age = this.age;
  return obj;
 }
}

0 0
原创粉丝点击