首页 > 代码库 > 原型模式
原型模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
原型模式
*/
namespace App_MYCS.HDL_SJMS.YXMS
{
class my_YXMS
{
public void dy()
{
ConcretePrototype1 p1 = new ConcretePrototype1("I");
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine("cloned:{0}",c1.Id);
}
}
abstract class prototype
{
private string id;
public string Id
{
get { return id; }
set { id = value; }
}
public prototype(string id)
{
this.Id = id;
}
public abstract prototype Clone();
}
class ConcretePrototype1 : prototype
{
public ConcretePrototype1(string id)
: base(id)
{
}
public override prototype Clone()
{
return (prototype)this.MemberwiseClone();//注意这里是引用传递
}
}
////////////////////////////////////////////////////////////////////////////////
//ICloneable 接口 定义了Clone()方法
class class_yxms1 : ICloneable
{
private string str1;
public string Str1
{
get { return str1; }
set { str1 = value; }
}
private string str2;
public string Str2
{
get { return str2; }
set { str2 = value; }
}
public class_yxms1(string str)
{
this.str1 = str;
}
public void SetData(string str1, string str2)
{
this.str1 = str1;
this.str2 = str2;
}
public void showdata()
{
Console.WriteLine(str1);
Console.WriteLine(str2);
}
#region ICloneable 成员
public object Clone()
{
return (object)this.MemberwiseClone();
}
#endregion
}
class class_yxms1_dy
{
public void dy()
{
class_yxms1 yx = new class_yxms1("我");
yx.SetData("26", "末婚");
class_yxms1 yx1 = (class_yxms1)yx.Clone();
yx1.SetData("27", "已婚");
yx.showdata();
yx1.showdata();
}
/*
正确输出
我末婚
我已婚
*/
}
//引用 浅复制
class class_yxms_entity
{
private string str1;
public string Str1
{
get { return str1; }
set { str1 = value; }
}
private string str2;
public string Str2
{
get { return str2; }
set { str2 = value; }
}
}
class class_YXMS2:ICloneable
{
private string str3;
private string str4;
private class_yxms_entity entity;
public class_YXMS2(string str)
{
this.str3 = str;
entity = new class_yxms_entity();
}
public void SetData(string str,string str1)
{
entity.Str1 = str;
entity.Str2 = str1;
}
public void showdata()
{
Console.WriteLine(str3);
Console.WriteLine(str4);
Console.WriteLine(entity.Str1+entity.Str2);
}
#region ICloneable 成员
public object Clone()
{
return (object)this.MemberwiseClone();
}
#endregion
}
class class_yxms2_dy
{
public void dy()
{
class_YXMS2 yx = new class_YXMS2("我");
yx.SetData("26", "末婚");
class_YXMS2 yx1 = (class_YXMS2)yx.Clone();
yx1.SetData("27", "已婚");
yx.showdata();
yx1.showdata();
}
/*
正确输出
27", "已婚
27", "已婚
*
输出对象中的class_yxms_entity被复制新对象引用,所以改变后的值也变了
*/
}
//传值 深复制
//引用 浅复制
class class_yxms_entity2 : ICloneable
{
private string str1;
public string Str1
{
get { return str1; }
set { str1 = value; }
}
private string str2;
public string Str2
{
get { return str2; }
set { str2 = value; }
}
#region ICloneable 成员
public object Clone()
{
return (object)this.MemberwiseClone();
}
#endregion
}
class class_YXMS3 : ICloneable
{
private string str3;
private string str4;
private class_yxms_entity2 entity;
public class_YXMS3(string str)
{
this.str3 = str;
entity = new class_yxms_entity2();
}
private class_YXMS3(class_yxms_entity2 entity)
{
this.entity = (class_yxms_entity2)entity.Clone();
}
public void SetData(string str, string str1)
{
entity.Str1 = str;
entity.Str2 = str1;
}
public void showdata()
{
Console.WriteLine(str3);
Console.WriteLine(str4);
Console.WriteLine(entity.Str1 + entity.Str2);
}
#region ICloneable 成员
public object Clone()
{
class_YXMS3 yxms3 = (class_YXMS3)this.MemberwiseClone();
yxms3.str3 = this.str3;
yxms3.str4 = this.str4;
return yxms3;
}
#endregion
}
}
原型模式