原型模式( Prototype Pattern)
来源:互联网 发布:网络电玩城捕鱼 编辑:程序博客网 时间:2024/05/16 03:27
一、原型模式简介(Brief Introduction)
原型模式(Prototype Pattern):用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。
Specify the kind of objects to create using a prototypical instance, and create new objects by copying this prototype。
浅复制与深复制区别:
浅复制,被复制的所有变量都还有与原来对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
深复制,把引用对象的变量指向复制过的新对象,而不是原有的被引用的对象。
Net命名空间System提供了一个IConeable接口,此接口只有一个方法Clone(),只需要实现这个接口就可以实现原型模式(Prototype Pattern)了。
二、解决的问题(What To Solve)
当一个对象生成不是通过New而是通过复制旧对象的时候,可以考虑使用原型模式。
三、原型模式分析(Analysis)
1、原型模式结构
Prototype类:原型类 Clone()方法:克隆自身的接口。
ConcretePrototypeA、ConcretePrototypeA类:原型类的具体实现。克隆一个自身的操作。
2、代码
1、原型抽象类Prototype
///<summary>
///抽象原型模式类
///</summary>
public abstractclassPrototype
{
private string _id;
public Prototype(string id)
{
this.Id = id;
}
public string Id
{
get { return _id; }
set { _id = value; }
}
public abstract Prototype Clone();
}
2、具体实现类ConcretePrototypeA和ConcretePrototypeB
public classConcretePrototypeA :Prototype
{
public ConcretePrototypeA(string id)
: base(id)
{ }
///<summary>
/// Returns a shallow copy浅拷贝
///</summary>
///<returns>a shallow copy</returns>
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
public classConcretePrototypeB:Prototype
{
public ConcretePrototypeB(string id)
: base(id)
{ }
///<summary>
/// a shallow copy
///</summary>
///<returns>浅拷贝</returns>
public override Prototype Clone()
{
return (Prototype)this.MemberwiseClone();
}
}
2、客户端代码
static void Main(string[] args)
{
Prototype pA = new ConcretePrototypeA("A");
Prototype cA = pA.Clone() as ConcretePrototypeA ;
Console.WriteLine("Cloned:"+cA.Id);
ConcretePrototypeB pB = new ConcretePrototypeB("B");
ConcretePrototypeB cB = (ConcretePrototypeB)pB.Clone();
Console.WriteLine("Cloned:"+cB.Id);
Console.ReadKey();
}
3、实例运行结果
四.原型模式实例分析(Example)
1、场景
颜色索引器存储多种颜色值,从颜色索引器中克隆客户需要几种颜色。结构如下图所示
ColorManager类:颜色索引器
ColorPrototype类:原型模式抽象类
Color类:原型模式抽象类的具体实现,Clone方法的实现,克隆自身的操作
2、代码
1、原型模式抽象类ColorPrototype及其具体实现类Color
///<summary>
///原型模式抽象类
///</summary>
public abstractclassColorPrototype
{
public abstract ColorPrototype Clone();
}
///<summary>
///具体实现类
///</summary>
public classColor :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;
}
///<summary>
///实现浅复制
///</summary>
///<returns></returns>
public override ColorPrototype Clone()
{
Console.WriteLine("Cloning color RGB: {0,3},{1,3},{2,3}", _red, _green, _blue);
return this.MemberwiseClone() as ColorPrototype;
}
}
3、客户端代码
static void Main(string[] args)
{
ColorManager colormanager = new ColorManager();
//初始化标准的red green blue颜色。
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(255, 54, 0);
colormanager["peace"] = new Color(128, 211, 128);
colormanager["flame"] = new Color(211, 34, 20);
// 克隆颜色
Color color1 = colormanager["red"].Clone()asColor;
Color color2 = colormanager["peace"].Clone()asColor;
Color color3 = colormanager["flame"].Clone()asColor;
Console.ReadKey();
}
3、实例运行结果
五、总结(Summary)
本文对原型模式(Prototype Pattern)的概念、设计结构图、代码、使用场景、深复制与浅复制的区别,以及如何Net平台下实现克隆进行了描述。以一个实例进行了说明。原型模式是比较常用和简单的设计模式。
- 原型模式(Prototype Pattern)
- 原型模式(Prototype Pattern)
- 原型模式(Prototype Pattern)
- 原型模式 Prototype Pattern
- 原型模式 Prototype Pattern
- 原型模式(Prototype Pattern)
- 原型模式( Prototype Pattern)
- 原型模式【PROTOTYPE PATTERN 】
- 原型模式(prototype pattern)
- 原型模式(Prototype Pattern)
- 原型模式(prototype pattern)
- 原型模式Prototype Pattern
- 原型模式(Prototype Pattern)
- 原型模式【Prototype Pattern】
- 原型模式(Prototype Pattern)
- 原型模式【Prototype Pattern】
- 原型模式-Prototype Pattern
- 原型模式(Prototype Pattern)
- 雁过也,月满西楼
- 适配器模式(Adapter Pattern)
- 向android工程的res里边的drawable文件夹导入图片,以便工程使用
- 单例模式( Singleton Pattern)
- 常见Oracle HINT的用法
- 原型模式( Prototype Pattern)
- 建造者模式(Builder Pattern)
- 工厂方法模式( Factory Method Pattern)
- 程序员:编程能力 vs 职业技能,哪个更重要
- 简单工厂模式(Simple Factory Pattern)
- [一分钟先生]袁斌:项目管理中风险控制、成本控制的重要实践
- OLEDBDepartmentATL
- 观察者模式(Observer Pattern)
- UbuntuX下的终端上网