zoukankan      html  css  js  c++  java
  • 浅尝DesignPattern_Prototype

    UML:

    Sample:

  • Prototype  ()
    • declares an interface for cloning itself
    • 定义复制对象的接口
  • ConcretePrototype  ()
    • implements an operation for cloning itself
    • 实现复制自己的操作
  • Client  ()
    • creates a new object by asking a prototype to clone itself
    • 通过访问prototype来创建一个新的对象来复制

    Prototype:

    1 abstract class Prototype
    2 {
    3 private string _id;
    4 public Prototype(string id)
    5 {
    6 this._id = id;
    7 }
    8 public string Id
    9 {
    10 get { return _id; }
    11 }
    12 public abstract Prototype Clone();
    13 }

    ConcretePrototype1:

    1 class ConcretePrototype1:Prototype
    2 {
    3 public ConcretePrototype1(string id)
    4 : base(id)
    5 { }
    6
    7 public override Prototype Clone()
    8 {
    9 return (Prototype)this.MemberwiseClone();
    10 }
    11 }
    ConcretePrototype2:
    1 class ConcretePrototype2
    2 {
    3 public ConcretePrototype2(string id)
    4 : base(id)
    5 { }
    6 public override Prototype Clone()
    7 {
    8 return (Prototype)this.MemberwiseClone();
    9 }
    10 }
    Program:
    1 #region Prototype
    2 ConcretePrototype1 p1 = new ConcretePrototype1("I");
    3 ConcretePrototype1 c1 = (ConcretePrototype1)p1.Clone();
    4 Console.WriteLine("Cloned: {0}", c1.Id);
    5 ConcretePrototype2 p2 = new ConcretePrototype2("II");
    6 ConcretePrototype2 c2 = (ConcretePrototype2)p2.Clone();
    7 Console.WriteLine("Cloned: {0}", c2.Id);
    8 Console.ReadKey();
    9 #endregion
    原型模式就是请求一个原型来克隆对象自身
查看全文
  • 相关阅读:
    Java实现 蓝桥杯VIP 算法提高 阮小二买彩票
    Java实现 蓝桥杯VIP 算法提高 传染病控制
    Java实现 蓝桥杯VIP 算法提高 传染病控制
    Java实现 蓝桥杯VIP 算法提高 传染病控制
    Java实现 蓝桥杯VIP 算法提高 传染病控制
    Java实现 蓝桥杯VIP 算法提高 传染病控制
    Java实现 蓝桥杯VIP 算法提高 企业奖金发放
    Java实现 蓝桥杯VIP 算法提高 企业奖金发放
    让程序后台隐藏运行
    只要你喜欢,并且可以养家糊口,就是好的
  • 原文地址:https://www.cnblogs.com/TivonStone/p/1721590.html
  • Copyright © 2011-2022 走看看