经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » 设计模式 » 查看文章
设计模式系列 - 原型模式
来源:cnblogs  作者:hippieZhou  时间:2018/12/10 9:42:05  对本文有异议

所谓原型模式是指为创建重复对象提供一种新的可能。

介绍

当面对系统资源紧缺的情况下,如果我们在重新创建一个新的完全一样的对象从某种意义上来讲是资源的浪费,因为在新对象的创建过程中,是会有系统资源的消耗,而为了尽可能的节省系统资源,我们有必要寻找一种新的方式来创建重复对象。

类图描述

由于Shape 抽象类继承了 ICloneable 接口,所以通过上图我们可以发现,所有具体的类型都继承 Shape 抽象类,并实现 Clone() 方法即可。

代码实现

1、定义具有抽象基类

  1. public abstract class Shape : ICloneable
  2. {
  3. private string id;
  4. protected string type;
  5. public abstract void Draw();
  6. public string GetId() => id;
  7. public string GetType() => type;
  8. public void SetId(string id) => this.id = id;
  9. public new object MemberwiseClone() => base.MemberwiseClone();
  10. public abstract object Clone();
  11. }

2、定义具体类型

  1. public class Circle:Shape
  2. {
  3. public Circle() => type = "Circle";
  4. public override void Draw()
  5. {
  6. Console.WriteLine("I am a Circle");
  7. }
  8. public override object Clone()
  9. {
  10. Circle obj = new Circle();
  11. obj.type = type;
  12. obj.SetId(this.GetId());
  13. return obj;
  14. }
  15. }
  16. public class Rectangle:Shape
  17. {
  18. public Rectangle() => type = "Rectangle";
  19. public override void Draw()
  20. {
  21. Console.WriteLine("I am a Rectangle");
  22. }
  23. public override object Clone()
  24. {
  25. Rectangle obj = new Rectangle();
  26. obj.type = type;
  27. obj.SetId(this.GetId());
  28. return obj;
  29. }
  30. }
  31. public class Square:Shape
  32. {
  33. public Square() => type = "Square";
  34. public override void Draw()
  35. {
  36. Console.WriteLine("I am a Square");
  37. }
  38. public override object Clone()
  39. {
  40. Square obj = new Square();
  41. obj.type = type;
  42. obj.SetId(this.GetId());
  43. return obj;
  44. }
  45. }

3、创建种子数据

  1. public class ShapeCache
  2. {
  3. private static HashSet<Shape> shapeMap = new HashSet<Shape>();
  4. public static Shape GetShape(string shapeId)
  5. {
  6. var cachedShape = shapeMap.FirstOrDefault(p => p.GetId() == shapeId);
  7. return (Shape) cachedShape?.Clone();
  8. }
  9. public static void LoadCache()
  10. {
  11. Circle circle = new Circle();
  12. circle.SetId("1");
  13. shapeMap.Add(circle);
  14. Square square = new Square();
  15. square.SetId("2");
  16. shapeMap.Add(square);
  17. Rectangle rectangle = new Rectangle();
  18. rectangle.SetId("3");
  19. shapeMap.Add(rectangle);
  20. }
  21. }

4、上层调用

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. ShapeCache.LoadCache();
  6. Shape clonedShape1 = (Shape) ShapeCache.GetShape("1");
  7. Console.WriteLine(clonedShape1.GetType());
  8. clonedShape1.Draw();
  9. Shape clonedShape2 = (Shape)ShapeCache.GetShape("2");
  10. Console.WriteLine(clonedShape2.GetType());
  11. clonedShape2.Draw();
  12. Shape clonedShape3 = (Shape)ShapeCache.GetShape("3");
  13. Console.WriteLine(clonedShape3.GetType());
  14. clonedShape3.Draw();
  15. Console.ReadKey();
  16. }
  17. }

总结

C# 中实现原型模式的关键是需要定义一个继承 ICloneable 接口的抽象类,并在子类中重写相应的 Clone() 方法即可。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号