-
Prototype 模式
属于创建型模式
通过浅拷贝一个已经存在的对象,来创建一个对象,主要用于当对象的数据获取比较复杂,或获取对象成本较高时使用。 -
代码示例
/*
*protoType pattern
* Prototype pattern in which new objects
* are created by copying pre-existing objects (prototypes) of the same class.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Pattern02
{
class Program
{
static void Main(string[] args)
{
// Create two instances and clone each
ConcretePrototype1 p1 = new ConcretePrototype1("A");
ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
Console.WriteLine("cloned:{0}", c1.Id);
ConcretePrototype2 p2 = new ConcretePrototype2("B");
ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
Console.WriteLine("cloned:{0}", c2.Id);
Console.ReadLine();
}
}
abstract class Prototype
{
private string id;//
public string Id
{
get { return id; }
}
public Prototype(string id)
{
this.id = id;
}
public abstract Prototype Clone();
}
class ConcretePrototype1 : Prototype
{
public ConcretePrototype1(string id):base(id)
{ }
//返回一个shallow copy的prototype类型的对象
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
class ConcretePrototype2 : Prototype
{
public ConcretePrototype2(string id) : base(id)
{ }
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
}
- 例如 使用prototype 获取color 对象
/*
*protoType pattern
* Prototype pattern in which new objects
* are created by copying pre-existing objects (prototypes) of the same class.
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Pattern02
{
class Program
{
static void Main(string[] args)
{
ColorManager colorManager = new ColorManager();
colorManager["red"] = new Color(255, 0, 0);
colorManager["green"] = new Color(0, 255, 0);
colorManager["blue"] = new Color(0, 0, 255);
colorManager["angry"] = new Color(225, 54, 0);
colorManager["peace"] = new Color(128, 211, 128);
colorManager["flame"] = new Color(211, 34, 20);
///通过shallow copy 获取对象。
Color color1 = colorManager["red"].Clone() as Color;
Color color2 = colorManager["peace"].Clone() as Color;
Color color3 = colorManager["flame"].Clone() as Color;
Console.ReadLine();
}
}
/// <summary>
/// prototype 定义一个接口,用于shallow copy
/// </summary>
abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}
/// <summary>
/// concretePrototype 实现接口,用于shallow copy
/// </summary>
class Color : ColorPrototype
{
private int _red;
private int _green;
private int _blue;
public Color(int red,int green, int blue)
{
this._red = red;
this._green = green;
this._blue = blue;
}
//返回一个shallow copy 获取到的ColorPrototype 对象
public override ColorPrototype Clone()
{
Console.WriteLine(
"Cloning color RGB: {0,3},{1,3},{2,3}",
_red, _green, _blue);
return this.MemberwiseClone() as ColorPrototype;
}
}
/// <summary>
/// client 通过protoType的shallow copy 得到新的对象。
/// </summary>
class ColorManager
{
private Dictionary<string, ColorPrototype> _colors = new Dictionary<string, ColorPrototype>();
//indexer 索引器
public ColorPrototype this[string key]
{
get { return _colors[key]; }
set { _colors.Add(key, value); }
}
}
}