当前位置:   article > 正文

C#设计模式之4-原型模式_c# 原型模式

c# 原型模式

原型模式(Prototype Pattern)

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/395 访问。

原型模式属于创建型模式,使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象。

原型模式关注的是大量相同或相似对象的创建问题。应用原型模式就是建立一个原型,然后通过对原型来进行复制的方法,产生一个和原型相同或相似的新对象。

角色:

1、抽象原型(Prototype)

声明一个克隆自身的接口,通常名为Clone;

2、具体原型(Concrete Prototype)

实现一个克隆自身的操作,包含深拷贝和浅拷贝。

浅克隆(Shallow Clone):当原型对象被复制时,只复制它本身和其中包含的值类型的成员变量,而引用类型的成员变量并没有复制,它们只是指向同一个引用。
深克隆(Deep Clone):除了对象本身被复制外,对象所包含的所有成员变量也将被复制,它们拥有不同的副本。

注:C#中的MemberwiseClone属于浅克隆。

示例:

命名空间PrototypePattern包含细胞基类Cell,它的2个实现类分别为:PlantCell植物细胞类和Animal动物细胞类,另外包含CloneBase泛型基类。本案例尝试模拟细胞的分裂过程以展示原型模式在复制对象本身方面的独到之处。

namespace PrototypePattern
  1. [Serializable]
  2. public abstract class Cell : CloneBase<Cell> {
  3. public int Id { get; set; }
  4. public string Wall { get; set; }//细胞壁
  5. public string Membrane { get; set; }//细胞膜
  6. public string Cytoplasm { get; set; }//细胞质
  7. public string Nucleus { get; set; }//细胞核
  8. public Content Content { get; set; } = new Content();//细胞器
  9. public Cell(int id,
  10. string wall,
  11. string membrane,
  12. string cytoplasm,
  13. string nucleus) {
  14. this.Id = id;
  15. this.Wall = wall;
  16. this.Membrane = membrane;
  17. this.Cytoplasm = cytoplasm;
  18. this.Nucleus = nucleus;
  19. this.Content = new Content();
  20. }
  21. public abstract Cell Division();
  22. }

抽象细胞基类Cell,继承自CloneBase并定义Division分裂接口。

  1. [Serializable]
  2. public class PlantCell : Cell {
  3. public PlantCell(int id,
  4. string wall,
  5. string membrane,
  6. string cytoplasm,
  7. string nucleus)
  8. : base(id, wall, membrane, cytoplasm, nucleus) {
  9. }
  10. public override Cell Division() {
  11. var cell = this.MemberwiseClone() as Cell;
  12. cell.Id = RandomUtil.RandomNum();
  13. return cell;
  14. }
  15. }

植物细胞类PlantCell,细胞基类的具体实现类,标记Serializable特性以支持序列化的深克隆。

  1. [Serializable]
  2. public class AnimalCell : Cell {
  3. public AnimalCell(int id,
  4. string wall,
  5. string membrane,
  6. string cytoplasm,
  7. string nucleus)
  8. : base(id, wall, membrane, cytoplasm, nucleus) {
  9. }
  10. public override Cell Division() {
  11. var cell = this.MemberwiseClone() as Cell;
  12. cell.Id = RandomUtil.RandomNum();
  13. return cell;
  14. }
  15. }

动物细胞类AnimalCell,细胞基类的具体实现类,标记Serializable特性以支持序列化的深克隆。

  1. [Serializable]
  2. public class Content {
  3. public string Mitochondria { get; set; }//线粒体
  4. public int Chloroplasts { get; set; }//叶绿体
  5. public int EndoplasmicReticulum { get; set; }//内质网
  6. public int GolgiBody { get; set; }//高尔基复合体
  7. public int Ribosomes { get; set; }//核糖体
  8. public int Centrosome { get; set; }//中心体
  9. public int Vacuole { get; set; }//液泡
  10. public int Lysosomes { get; set; }//溶酶体
  11. public int Microtubule { get; set; }//微管
  12. }

细胞质类Content,为细胞基类中所包含的一个对象成员。

  1. [Serializable]
  2. public class CloneBase<T> {
  3. public virtual T Clone() {
  4. var memoryStream = new MemoryStream();
  5. var formatter = new BinaryFormatter();
  6. formatter.Serialize(memoryStream, this);
  7. memoryStream.Position = 0;
  8. return (T)formatter.Deserialize(memoryStream);
  9. }
  10. }

克隆类CloneBase,包含一个虚拟的Clone方法以支持深克隆。

  1. public class RandomUtil {
  2. public static int RandomNum() {
  3. return new Random().Next(1000000, 10000000);
  4. }
  5. }

产生细胞Id的工具类,从100万到1000万。

  1. public class Program {
  2. private static Cell _cell = null;
  3. private const string SPLIT_BREAK =
  4. "-----------------------------------------------------";
  5. public static void Main(string[] args) {
  6. _cell = new PlantCell(RandomUtil.RandomNum(),
  7. "wall",
  8. "membrane",
  9. "cytoplasm",
  10. "nucleus");
  11. var plant = _cell.Division();
  12. Console.WriteLine($"_cell.GUID:{_cell.Id},{Environment.NewLine}plant.GUID:{plant.Id}," +
  13. $"{Environment.NewLine}equals:{_cell.Id == plant.Id}.");
  14. Console.WriteLine(SPLIT_BREAK);
  15. _cell.Content.Mitochondria = "10010101010100101010101";
  16. Console.WriteLine($"_cell.Content.Mitochondria:{_cell.Content.Mitochondria},\r\n" +
  17. $"plant.Content.Mitochondria:{plant.Content.Mitochondria}," +
  18. $"{Environment.NewLine}equals:" +
  19. $"{_cell.Content.Mitochondria == plant.Content.Mitochondria}.");
  20. Console.WriteLine(SPLIT_BREAK);
  21. var animal = _cell.Clone();
  22. Console.WriteLine($"_cell.GUID:{_cell.Id},{Environment.NewLine}animal.GUID:{animal.Id}," +
  23. $"{Environment.NewLine}equals:{_cell.Id == animal.Id}.");
  24. Console.WriteLine(SPLIT_BREAK);
  25. _cell.Content.Mitochondria = "01001010010100101010010";
  26. Console.WriteLine($"_cell.Content.Mitochondria:{_cell.Content.Mitochondria},\r\n" +
  27. $"animal.Content.Mitochondria:{animal.Content.Mitochondria}," +
  28. $"{Environment.NewLine}equals:" +
  29. $"{_cell.Content.Mitochondria == animal.Content.Mitochondria}.");
  30. Console.WriteLine(SPLIT_BREAK);
  31. Console.ReadKey();
  32. }
  33. }

以上是调用方的代码,植物细胞实例调用了浅克隆,而动物细胞实例调用了深克隆,请仔细分析这段代码。以下是这个案例的输出结果:

  1. _cell.GUID:6768270,
  2. plant.GUID:2028096,
  3. equals:False.
  4. -----------------------------------------------------
  5. _cell.Content.Mitochondria:10010101010100101010101,
  6. plant.Content.Mitochondria:10010101010100101010101,
  7. equals:True.
  8. -----------------------------------------------------
  9. _cell.GUID:6768270,
  10. animal.GUID:6768270,
  11. equals:True.
  12. -----------------------------------------------------
  13. _cell.Content.Mitochondria:01001010010100101010010,
  14. animal.Content.Mitochondria:10010101010100101010101,
  15. equals:False.
  16. -----------------------------------------------------

优点:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/395 访问。

1、原型模式向客户隐藏了创建新实例的复杂性;
2、原型模式允许动态增加或较少产品类;
3、原型模式简化了实例的创建结构,工厂方法模式需要有一个与产品类等级结构相同的等级结构,而原型模式不需要这样;
4、产品类不需要事先确定产品的等级结构,因为原型模式适用于任何的等级结构。

缺点:

1、每个类必须配备一个克隆方法或继承自CloneBase泛型类;
2、配备克隆方法需要对类的功能进行通盘考虑,这对于全新的类不是很难,但对于已有的类不一定很容易,特别当一个类引用不支持串行化的间接对象,或者引用含有循环结构的时候。

使用场景:

1、 当一个系统应该独立于它的产品创建、构成和表示时;
2、当要实例化的类是在运行时刻指定时,例如通过动态装载来创建一个类;
3、为了避免创建一个与产品类层次平行的工厂类层次时;
4、当一个类的实例只能有几个不同状态组合中的一种时。建立相应数目的原型并Clone它们可能比每次用合适的状态手工实例化该类更方便一些。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/487364
推荐阅读
相关标签
  

闽ICP备14008679号