原型设计模式--寻源之旅
总结下~~:->/Files/ajaxren/寻源之旅.rar
1针对接口编程,而不是针对实现编程
-客户无须知道所使用对象的特定类型,只需要知道对象有客户所期望的接口
2优先使用对象组合,而不是类继
承继承通常为"白箱复用",对象组合称为黑箱复用,继承在某种程度上
破坏了类的封装性,子类父类耦合度高,对象组合只要求被组合的对象有良好的接口,耦合低
3封装变化点
使用封装来创建对象之间的分界层,让设计者可以在分界层的一端修改而不影响另一端
--ok废话多了点~~请注意红色的地方~~
这个模式就是利用克隆来得到新的对象,克隆就是说所有的成员都会连同们保存的数据一起被克隆
,实际上就是开辟一个新的地址比如
{
public abstract NormalActrow Clone();
}
public class NormalActrowA : NormalActrow
{
public p pi = new p();
public int a = 0;
public override NormalActrow Clone()
{
this.MemberwiseClone();
}
public class test
{
public static void Main()
{
NormalActrowA t=new NormalActrowA();
NormalActrowA t1=t.Clone();
}
}
上面是标准的做法,确实没有什么问题- -很好,但是如果克隆的类有个非值类型,则不克隆,而是直接和被克隆的对象引用同一地址,- -换句话说,就是找不到源头。
上图第一部分是浅拷贝,当有个非值类型-〉200出现的时候,上下同时引用,
上图第二部分深层拷贝则是不会出现这个问题- -找到源头了
//--测试代码
using System.Collections.Generic;
using System.Text;
namespace Clr.ProtoType.d
{
[Serializable]
public abstract class NormalActrow
{
public abstract NormalActrow Clone(string isAllNumber);
}
[Serializable]
public class NormalActrowA : NormalActrow
{
public p pi = new p();
public int a = 0;
public override NormalActrow Clone(string isAllNumber)
{
if (isAllNumber == "A")
{
return CreateActrowA();
}
if (isAllNumber == "B")
{
return CreateActrowB();
} if (isAllNumber == "C")
{
return CreateActrowC();
} if (isAllNumber == "D")
{
return CreateActrowD();
}
return CreateActrowA();
}
//实现深拷贝-这个是从设计模式中得到的灵感就是,我看单例设计模式时书上有这么一条不可再单例模式上使用序列,变相就是说序列化后生成的时完整的拷贝对象咯~~
public NormalActrow CreateActrowA()
{
NormalActrowA na1 = (NormalActrowA)this.MemberwiseClone();
na1.pi = (p)this.pi.Clone();
na1.pi.b = 13343;
Console.WriteLine("经过克隆更改的数据:{0}", ((NormalActrowA)na1).pi.b);
Console.WriteLine();
Console.WriteLine("原始数据:{0}", this.pi.b);
return na1;
}
//实现深拷贝-这个是从设计模式中得到的灵感就是,我看单例设计模式时书上有这么一条不可再单例模式上使用序列,变相就是说序列化后生成的时完整的拷贝对象咯~~
public NormalActrowA CreateActrowB()
{
NormalActrowA na1;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(memoryStream, this);
memoryStream.Position = 0;
//--利用反序列化
na1 = (NormalActrowA)formatter.Deserialize(memoryStream);
na1.pi.b = 12;
Console.WriteLine("经过克隆更改的数据:{0}", ((NormalActrowA)na1).pi.b);
Console.WriteLine();
Console.WriteLine("原始数据 :{0}", this.pi.b);
return na1;
}
public NormalActrowA CreateActrowC()
{
NormalActrowA na1 = new NormalActrowA();
na1.a = this.a;
na1.pi.b = this.pi.b;
na1.pi.S1 = this.pi.S1;
na1.pi.T = this.pi.T;
na1.pi["dd"] = na1.pi["dd"];
na1.pi.b = 123;
Console.WriteLine("经过克隆更改的数据:{0}", na1.pi.b);
Console.WriteLine();
Console.WriteLine("原始数据 :{0}", this.pi.b);
Console.WriteLine();
return na1;
}
浅克隆
}
[Serializable]
public class p
{
public int b = 0;
public String S1 = @"cccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDD
cccccccccccccccccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccccccccccccccccDDDDcccDDDD";
private string _t = @"来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来
来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来
来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来来
";
public string T
{
get
{
return _t;
}
set { _t = value; }
}
private string _i = "";
public string this[string b]
{
get { return _i; }
set { _i = value; }
}
public p Clone()
{
return (p)this.MemberwiseClone();
}
}
public class HRSystem
{
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceCounter(ref long count);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
static extern bool QueryPerformanceFrequency(ref long count);
[MethodImplAttribute(MethodImplAttribute.MethodImplOptions.a)]
[STAThread]
public static void Main()
{
Console.WriteLine("请输入:");
Console.WriteLine("a:深层拷贝(自己克隆在调用引用对象的克隆);");
Console.WriteLine("b:深层拷贝(序列化);");
Console.WriteLine("c:深层拷贝(实力一个新的对象,包括自己和引用对象);");
Console.WriteLine("d:浅层拷贝;");
Console.WriteLine("输入其它退出");
while (true)
{
Console.WriteLine("--------------------------------------------------------------");
Console.WriteLine("");
long count = 0;
long count1 = 0;
long freq = 0;
double result = 0;
QueryPerformanceFrequency(ref freq);
QueryPerformanceCounter(ref count);
string TOrF = Console.ReadLine().ToUpper();
if (TOrF == "A" || TOrF == "B" || TOrF == "C" || TOrF== "D")
{
NormalActrowA t = new NormalActrowA();
NormalActrow t2 = t.Clone(TOrF);
}
else
return;
QueryPerformanceCounter(ref count1);
count = count1 - count;
result = (double)(count) / (double)freq;
Console.WriteLine("耗时: {0} 秒", result);
Console.WriteLine("");
Console.WriteLine("输入T继续测试,输入其它字符退出:");
string chooic = Console.ReadLine();
if (chooic.ToUpper() != "T")
{
return;
}
}
}
}
[Serializable, AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor, Inherited = false)]
public sealed class MethodImplAttribute : Attribute
{
// Methods
public MethodImplAttribute()
{ }
public MethodImplAttribute(short value)
{ }
internal MethodImplAttribute(MethodImplAttributes methodImplAttributes)
{ }
public MethodImplAttribute(MethodImplOptions methodImplOptions)
{ }
// Properties
public MethodImplOptions Value { get { return MethodImplOptions.a; } }
public enum MethodImplOptions
{
a
}
internal enum MethodImplAttributes
{ b }
}
}
//--这个是测试结果
d<c<a<b
分析下:
d:是浅层拷贝
NormalActrowA na1 = (NormalActrowA)this.MemberwiseClone();
c:深层拷贝~一个元素一个元素的拷贝
NormalActrowA na1 = new NormalActrowA();
na1.a = this.a;
na1.pi.b = this.pi.b;
na1.pi.S1 = this.pi.S1;
na1.pi.T = this.pi.T;
na1.pi["dd"] = na1.pi["dd"];
a:深层拷贝:先拷自己,在拷贝引用--I Like
NormalActrowA na1 = (NormalActrowA)this.MemberwiseClone();
na1.pi = (p)this.pi.Clone();
c:序列化
NormalActrowA na1;
System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(memoryStream, this);
memoryStream.Position = 0;
//--利用反序列化
na1 = (NormalActrowA)formatter.Deserialize(memoryStream);
na1.pi.b = 12;
Console.WriteLine("经过克隆更改的数据:{0}", ((NormalActrowA)na1).pi.b);
Console.WriteLine();
Console.WriteLine("原始数据 :{0}", this.pi.b);
return na1;
我个人比较喜欢:a 性价比试中..如果您有更好的方法- -分享下谢谢咯