zoukankan      html  css  js  c++  java
  • 23种设计模式——Prototype模式

     Prototype模式是提供自我复制的功能。包括浅拷贝和深拷贝。

    一、Prototype模式的用途

    场景1:游戏场景中有很多类似的敌人,它们的技能都一样,但是随着敌人出现的位置和不同,它们的能力也不太一样。那么,可以创建一个敌人抽象类,然后对于不同能力的步兵创建不同的子类。然后,使用工厂模式让调用方依赖敌人抽象类。问题来了,如果有无数种能力不同的步兵,难道需要创建无数子类吗?还有步兵模式的初始化工作比较耗时,创建这么多步兵对象可能会浪费更多的时间。我们是不是只创建一个步兵模式,然后复制出更多的一模一样的步兵呢?复制后,只需要调整一下这些对象在地图出现的位置,或者调整一下他们的能力及其他特性即可。原型模式可以用来解决这类问题的。

    场景2:在商品房销售系统中,房屋信息是基础信息。在系统运行前必须输入房屋的各种信息到系统中,这是一项枯燥的重复劳动。如果让用户重复输入房间的类型、面积和卫生间样式,这个系统肯定尚未运行就夭折了。实际上,一个小区楼盘的样式并不多,不同的只是楼号。另外,楼盘中的房间类型也非常有限,从而为解决输入问题提供了启示。所以我们可以事先创建一个楼盘模型,然后复制出更多的楼盘模型。复制后,只需要调整一下楼号等信息即可。原型模式也可以用来解决这类问题。

    二、Portotype模式的结构

    三、代码如下

    Java代码

    吕震宇老师的例子很容易理解,故直接引用如下:

    // Prototype pattern -- Structural example  
    using System;

    // "Prototype"
    abstract class Prototype
    {
      
    // Fields
      private string id;

      
    // Constructors
      public Prototype( string id )
      
    {
        
    this.id = id;
      }


      
    public string Id
      
    {
        
    getreturn id; }
      }


      
    // Methods
      abstract public Prototype Clone();
    }


    // "ConcretePrototype1"
    class ConcretePrototype1 : Prototype
    {
      
    // Constructors
      public ConcretePrototype1( string id ) : base ( id ) {}

      
    // Methods
      override public Prototype Clone()
      
    {
        
    // Shallow copy
        return (Prototype)this.MemberwiseClone();
      }

    }


    // "ConcretePrototype2"
    class ConcretePrototype2 : Prototype
    {
      
    // Constructors
      public ConcretePrototype2( string id ) : base ( id ) {}

      
    // Methods
      override public Prototype Clone()
      
    {
        
    // Shallow copy
        return (Prototype)this.MemberwiseClone();
      }

    }


    /**//// <summary>
    /// Client test
    /// </summary>

    class Client
    {
      
    public static void Main( string[] args )
      
    {
        
    // Create two instances and clone each
        ConcretePrototype1 p1 = new ConcretePrototype1( "I" );
        ConcretePrototype1 c1 
    = (ConcretePrototype1)p1.Clone();
        Console.WriteLine( 
    "Cloned: {0}", c1.Id );

        ConcretePrototype2 p2 
    = new ConcretePrototype2( "II" );
        ConcretePrototype2 c2 
    = (ConcretePrototype2)p2.Clone();
        Console.WriteLine( 
    "Cloned: {0}", c2.Id );
      }

    }

    C++代码

    1. class CPrototype
    2. {
    3. public:
    4. virtual ~CPrototype() { }
    5. virtual CPrototype* Clone() = 0;
    6. };
    7. class CConcretePrototype1 : public CPrototype
    8. {
    9. public:
    10. CConcretePrototype1()
    11. {
    12. printf("[CConcretePrototype1] 构造函数. ");
    13. }
    14. ~CConcretePrototype1()
    15. {
    16. printf("[CConcretePrototype1] 析构函数. ");
    17. }
    18. public:
    19. virtual CPrototype* Clone() { return new CConcretePrototype1(*this); }
    20. private:
    21. // 既然有Clone函数,就将复制构成函数设置为私有的
    22. CConcretePrototype1(const CConcretePrototype1& rhs)
    23. {
    24. printf("[CConcretePrototype1] 复制构造函数. ");
    25. }
    26. };
    27. class CConcretePrototype2 : public CPrototype
    28. {
    29. public:
    30. CConcretePrototype2()
    31. {
    32. printf("[CConcretePrototype2] 构造函数. ");
    33. }
    34. ~CConcretePrototype2()
    35. {
    36. printf("[CConcretePrototype2] 析构函数. ");
    37. }
    38. public:
    39. virtual CPrototype* Clone() { return new CConcretePrototype2(*this); }
    40. private:
    41. // 既然有Clone函数,就将复制构成函数设置为私有的
    42. CConcretePrototype2(const CConcretePrototype2& rhs)
    43. {
    44. printf("[CConcretePrototype2] 复制构造函数. ");
    45. }
    46. };

    测试Demo代码

    1. void PrototypeDemo()
    2. {
    3. CPrototype* pItem = new CConcretePrototype1();
    4. CPrototype* pItem2 = pItem->Clone();
    5. CPrototype* pItem3 = pItem->Clone();
    6. delete pItem3;
    7. delete pItem2;
    8. delete pItem;
    9. }

    转:http://www.cnblogs.com/feipeng/archive/2007/03/13/672791.html

  • 相关阅读:
    为了我们自己的利益,请不要去支持番茄花园。
    游戏版本比较的算法[ZZ]
    DXUT框架剖析(9)
    强制删除任意文件以及文件夹
    安全幻想曲2008
    DXUT框架剖析(12)
    DXUT框架剖析(6)
    [Ph4nt0m] [zz]The Emergence Of A Theme
    俄国农民乘法
    写在msn签名上的I'M 计划
  • 原文地址:https://www.cnblogs.com/jpfss/p/9447495.html
Copyright © 2011-2022 走看看