zoukankan      html  css  js  c++  java
  • 4_Prototype 原型

    #Prototype
    
    ```
    // 不好的做法
        monster
            ghost
            demon
            sorcerer
            
    class Spawner
    {
    public:
      virtual ~Spawner() {}
      virtual Monster* spawnMonster() = 0;
    };
    
    class GhostSpawner : public Spawner
    {
    public:
      virtual Monster* spawnMonster()
      {
        return new Ghost();
      }
    };
    
    ```
    
    原型可以spawn出一个类似自己的物件(克隆)
    
    ``` 
    class Monster
    {
    public:
      virtual ~Monster() {}
      virtual Monster* clone() = 0;
    
      // Other stuff...
    };
    
    class Ghost : public Monster {
    public:
      Ghost(int health, int speed)
      : health_(health),
        speed_(speed)
      {}
    
      virtual Monster* clone()
      {
        return new Ghost(health_, speed_);
      }
    
    private:
      int health_;
      int speed_;
    };
    
    // 这样只需要一个spawn类
    class Spawner
    {
    public:
      Spawner(Monster* prototype)
      : prototype_(prototype)
      {}
    
      Monster* spawnMonster()
      {
        return prototype_->clone();
      }
    
    private:
      Monster* prototype_;
    };
    
    
    Monster* ghostPrototype = new Ghost(15, 3);
    Spawner* ghostSpawner = new Spawner(ghostPrototype);
    
    //template
    Spawner* ghostSpawner = new SpawnerFor<Ghost>();
    
    ```
  • 相关阅读:
    pip本地源搭建
    linux 创建 bootable iso 文件
    yum 源本地化 (two)
    linux 网络配置
    linux 设置root可以远程登陆
    察看linux 发行版
    mysql bin-log 设置
    samba 奇怪问题
    delphi中的临界区
    ligerGrid 取得选中行的数据
  • 原文地址:https://www.cnblogs.com/lightlfyan/p/4229301.html
Copyright © 2011-2022 走看看