using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Design_Patterns
{
//在软件系统中,经常面临着“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是它们却拥有比较稳定一致的接口。
//大量单据生成是可以使用 拷贝 (关于批量生成对象时可以使用原型模式)
//Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求 这些“易变类”拥有“稳定的接口”。
//Prototype模式对于“如何创建易变类的实体对象”采用“原型克隆”的方法来做,它使得我们可以非 常灵活地动态创建“拥有某些稳定接口”的新对象——所需工作仅仅是注册一个新类的对象(即 原型),然后在任何需要的地方不断地Clone。
//Prototype模式中的Clone方法可以利用.NET中的 Object类的MemberwiseClone()方法或者序列化来实现深拷贝。
//对象 剧烈变化 接口不变
abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}
class Color : ColorPrototype
{
int Red;
int Green;
int Blue;
public Color(int red, int green, int blue)
{
this.Red = red;
this.Green = green;
this.Blue = blue;
}
public override ColorPrototype Clone()
{
Console.WriteLine("Cloning color RGB:{0,3},{1,3},{2,3}", Red, Green, Blue);
return this.MemberwiseClone() as ColorPrototype;//浅表副本
}
}
class ColorManager
{
Hashtable colors = new Hashtable();
public ColorPrototype this[string name]
{
get
{
return colors[name] as ColorPrototype;
}
set
{
colors.Add(name, value);
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Design_Patterns
{
//在软件系统中,经常面临着“某些结构复杂的对象”的创建工作;由于需求的变化,这些对象经常面临着剧烈的变化,但是它们却拥有比较稳定一致的接口。
//大量单据生成是可以使用 拷贝 (关于批量生成对象时可以使用原型模式)
//Prototype模式同样用于隔离类对象的使用者和具体类型(易变类)之间的耦合关系,它同样要求 这些“易变类”拥有“稳定的接口”。
//Prototype模式对于“如何创建易变类的实体对象”采用“原型克隆”的方法来做,它使得我们可以非 常灵活地动态创建“拥有某些稳定接口”的新对象——所需工作仅仅是注册一个新类的对象(即 原型),然后在任何需要的地方不断地Clone。
//Prototype模式中的Clone方法可以利用.NET中的 Object类的MemberwiseClone()方法或者序列化来实现深拷贝。
//对象 剧烈变化 接口不变
abstract class ColorPrototype
{
public abstract ColorPrototype Clone();
}
class Color : ColorPrototype
{
int Red;
int Green;
int Blue;
public Color(int red, int green, int blue)
{
this.Red = red;
this.Green = green;
this.Blue = blue;
}
public override ColorPrototype Clone()
{
Console.WriteLine("Cloning color RGB:{0,3},{1,3},{2,3}", Red, Green, Blue);
return this.MemberwiseClone() as ColorPrototype;//浅表副本
}
}
class ColorManager
{
Hashtable colors = new Hashtable();
public ColorPrototype this[string name]
{
get
{
return colors[name] as ColorPrototype;
}
set
{
colors.Add(name, value);
}
}
}
}