创建型模式---抽象工厂模式

  • 例子--迷宫
    假定我们现在来实现一个迷宫,迷宫由一个个房间组成,而房间之间由墙和 门来连接。所以迷宫就有几个基础组件:房间,门,墙。假设他们都有一个共同的动作,进入(无论能否进入)。

    class MapSite {
        public:
          virtual void enter() = 0;
    };
    
    enum Direction { North, South, East, West };    // 房子4边
    
    class Room : public MapSite {
        public:
            Room(int roomNo)  {  }
            vois setSide(Direction, MapSite*) {   }     // 设置房子4边。
            virtual void enter()  {  }
    
        private:
            MapSite* _sides[4];    // 房子四边由墙或者门组成。
            int _roomNo;
    };
    
    class Wall:public MapSite {
        public:
          Wall()  {  }
          virtual void enter()  {  }
    };
    
    class Door {
        public:
            Door(Room* , Room*)  {  }
            virtual enter()  {  }
    
        private:
            Room* _room1;
            Room* _room2;
            bool _isOpen;
    };
    

    而迷宫本身就可以看成是一系列房间的集合。

    class Maze {
        public:
            Maze()  {  }
            void AddRoom(Room*)  {  }
            Room* RoomNo(int)  const;
      private:
            // 或用线性表,hash表,树存储一系列房间。
    };
    

    现在来定义一个类MazeGame,由它来创建迷宫

    class MazeGame {
        public:
            Maze* CreateMaze();
        private:
            Maze* aMaze;
    };
    
    Maze* MazeGame::CreateMaze()  {
        Maze* aMaze = new Maze();
    
        Room* r1 = new Room(1);
        Room* r2 = new Room(2);
        Door* theDoor = new Door(r1, r2);
        
        aMaze->addRoom(r1);
        aMaze->addRoom(r2);
        
        r1->setSide(North, new Wall());
        ......
        return aMaze;
      }
    

    这个函数的问题是在于它缺乏可扩展性,假设现在要增加一种新的游戏迷宫,施了魔法的迷宫,新增加2个组件,EnchantedRoom和NeedSpellDoor。需要咒语或者钥匙才能打开的门,和有宝物的房间(咒语,钥匙)。那么在MazeGame中是不是得另外新建一个函数CreateMagicMaze()?那假如我以后要新增n个新的游戏迷宫呢?

  • 抽象工厂模式

    试试这样写代码:

      class MazeFactory {
          public:
              MazeFactory();
    
              virtual Maze* makeMaze()  const  {  return new Maze();  }
              virtual Wall* makeWall()  const  {  return new Wall();  }
              virtual Room* makeRoom(int n)  const   {  return new Room(n);  }
              virtual Door* makeDoor(Room* r1, Room* r2)  const {  
                  return new Door(r1, r2);  
              }
      };
      
      class EnchantedMazeFactory : public MazeFactory {
          public:
              EnchantedMazeFactory();
    
              virtual Room* makeRoom(int n)  const   {  
                  return new EnchantedRoom(n, CastSpell()); 
              }
              virtual Door* makeDoor(Room* r1, Room* r2)  const {  
                  return new NeedSpellDoor(r1, r2);  
              }
      };
    
      Maze* MazeGame::CreateMaze(MazeFactory& factory) {
          Maze* aMaze = new Maze();
    
          Room* r1 = factory.makeRoom(1);
          Room* r2 = factory.makeRoom(2);
          Door* theDoor = factory.makeDoor(r1, r2);
        
          aMaze->addRoom(r1);
          aMaze->addRoom(r2);
        
          r1->setSide(North, factory.makeWall());
          ......
          return aMaze;
      }
    

你可能感兴趣的:(创建型模式---抽象工厂模式)