谈谈设计模式~原型模式(Prototype)

返回目录

原型模式是创建型模式的一种,其特点在于通过“复制”一个已经存在的实例来返回新的实例(clone),而不是新建(new)实例。被复制的实例就是我们所称的“原型”,这个原型是可定制的。

原型模式多用于创建复杂的或者耗时的实例,因为这种情况下,复制一个已经存在的实例使程序运行更高效;或者创建值相等,只是命名不一样的同类数据。

从原型模式的概念中,我们可以看到,在这个模式里,拷贝是个很重要的概念,即在不创建对象的情况下,返回一个已有对象,这就是拷贝去实现的,在面向对象的编程世界里,拷贝分为浅拷贝和深拷贝,如果希望对两者有更深入的认识,

可以阅读我的这篇文章《不忘本~浅拷贝和深拷贝》。

何时能用到它?

当你一个大对象被创建后,它可以在程序里被使用多次,而使用的时候可能有些属性值会发生变化,这里,你不需要重新去new这个对象,而可以使用原型模式去克隆这个对象,这样,可以提交程序的性能。

策略模式的结构图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXAAAAGCCAIAAAB2IQ96AAAgAElEQVR4nO2d/VcT1/7v9+r3/kf+2lX1W7qWP/T0l3va613rrluXB1OxVWQder8cj/fcWiu2IoIiak6pfThtfTqiVjT04KkUpZWaagtKgiEkJOEhEEIeyAx5gLk/7DAZJpNhkkySPeH9Wu/lmgyTZGQ+eWXvPTMbIgAAgE6QSu8AAKB6gFAAALoBoQAAdANCAQDoBoQCANANCAUAoBsQCgBANyAUAIBuQCgAAN2AUAAAugGhAAB0A0IBAOgGhAIA0A0IBQCgG5qE4p+d6e/v6+xo6exoeb+xrqG+Filz6C+/5/Z1n89T6prQnYWFwODDfvOFdtvoSGdHC/0fORx2LJdtmcbtckbCoZIe642Fcq/v7skTR252X3Y47C/GJ/yB2FyQnw/y84s0yzQBMaF1WQgtL4TlCYbjwXA8GElnMSuhqDzhaCKdpUwi0sQyiYrhkjRL0vDJJT4ZywrHJ7llmhQNLya+LsvSJNKJJ1ZkSSTXJSkmlU4qtZrOSiYr0qym4/V6nM4X9Fhc+uZzjouVtCz0IplI+Gdn7vXdvXb1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cyQcqvSOAAU4Lnaxq/NOz02ZTSYnJ4t8ZQgFlArb6Mjx5sNDQ4OV3hGwDtvoyEdH/5Jtk+KbJwKEAkoKx8UuffN5e1vz4MN+nEuuLD6fZ2ho8GJX5+n2j7N7OrrYRIBQQBlwu5zXrv7jxCcfNNTXIpXK8WOH/37hjOycjsj4+LguxxpCAeVjfHxcsZpBZSl+6EQEQgFlZWZmptIfH5BhbGxM3+MLoYDK4AGVphSHFUIBAOgGhAIA0A0IBQCgGxAKAEA3IBQAgG5AKAAA3YBQAAC6AaEAAHQDQgEA6AaEAgDQDQgFAKAbEAoAQDf+P7VWMQ+r0B8WAAAAAElFTkSuQmCC" alt="" />

策略模式实现说明

CarPrototype:抽象原型,定义一个克隆的方法,返回规定的原型

ConcteteCarPrototype:具体原型,实现了抽象原来的克隆,并返回了这类抽象原型,在具体原型中,还提供了某体的属性和方法等

CarManagerL:原型管理器,这个管理员用于被前台(UI)去调用,它用来存储原型集合

策略模式的C#实现

#region 原型模式
abstract class CarPrototype
{
public abstract CarPrototype Clone();
} class ConcteteCarPrototype : CarPrototype
{
private string _body, _color, _wheal;
public ConcteteCarPrototype(string body, string color, string wheal)
{
this._body = body;
this._color = color;
this._wheal = wheal;
}
public override CarPrototype Clone()
{
//实现浅表拷贝
return (CarPrototype)this.MemberwiseClone();
}
public void Display(string _carName)
{
Console.WriteLine("{0}'s Cart Values are: {1},{2},{3}",
_carName, _body, _color, _wheal);
}
} class CarManager
{
Hashtable colors = new Hashtable();
public CarPrototype this[string name]
{
get
{
return (CarPrototype)colors[name];
}
set
{
colors.Add(name, value);
}
}
} #endregion

调用代码

 CarManager colormanager = new CarManager();

            //初始化
colormanager["奥迪"] = new ConcteteCarPrototype("德国", "黑色", "四轮驱动");
colormanager["奇端"] = new ConcteteCarPrototype("中国", "白色", "前轮驱动"); //调用 ConcteteCarPrototype c1 = (ConcteteCarPrototype)colormanager["奇端"].Clone();
c1.Display("奇端"); Console.ReadLine();

结果截图

aaarticlea/png;base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sF4NOdB0K8FuMNNfaQZh/l7T1xN76QIkOlUb6jqgvgQme64mnoHGmY8PL/dGk2XsRUEJ0bvylP/UwbJa4irxt2T4jrWelRXlikbDljmWAujNTsP4mYdHRj2y9XXg6usPlyvYNivqZ/KfyP5fiUHz06FzNxy9dmW9eZTc1SCIdmELncZezolbxvNQwTt6DVr6pLyY64qvcY125Gsb7YsHqRh15LjV39fw97NfnToo3mI3reQH5VhfXUdQb/cgNkDNVvgkoLe1W2q58SwLD4FtsKTZ0yuvp/WjEteNHrXiqlDuJGwBb3GrfUraISbMb2ZNKxv+GUNByk/D2bbl0fcoEx3elPhYUdmu5bp17AXh+qOkG9QfbVwdRwqZIZkKzz5PabezKr/ifROvOsygW071WWS/Ur2YuEQP7m7SdN9ku3bARmXyc8LugxhMbz3ZJDsyGwjXula71IXXPxcUtwp146pcUqOxz5IrsskZ0HzPFkbGGIDxGdfXhY+6MgarGUkOzU7GGuDlx+PfZB/xkQQBOm+oMsQBLED6DIEQewAugxBEDuALkMQxA6gyxAEsQPoMgRB7AC6DEEQO4AuQxDEDqDLEASxA+gyBEHsALoMQRA7YPj/YerqASIIgkiALkMQxA6gyxAEsQPoMgRB7ADu/SMIYgfQZQiC2AF0GYIgdgBdhiCIHUCXIQhiB9BlCILYAXQZgiB2AF2GIIgdQJchCGIH0GUIgtgBdBmCIHYAXYYgiB1AlyEIYgfQZQiC2AF0GYIgdgBdhiCIHUCXIQhiB9BlCILYAXQZgiB2AF2GIIgdQJchCGIH0GUIgtgBdBmCIDbg/53xmLNPin/HAAAAAElFTkSuQmCC" alt="" />

返回目录

上一篇:url的内容及格式


下一篇:C#使用CLR/C++的DLL间接调用Native C++的DLL