笔记:Gof设计模式--Prototype

1、意图

    Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

 

2、适应性

    Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; and

    •  when the classes to instantiate are specified at run-time, for example, by dynamic loading;   or   

    •  to avoid building a class hierarchy of factories that parallels the class hierarchy of products;  or

    •  when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.

 

3、结构

 

 

4、示例代码

    We'll define a MazePrototypeFactory subclass of the MazeFactory class. MazePrototypeFactory will be initialized with prototypes of the objects it will create so that we don't have to subclass it just to change the classes of walls or rooms it creates.

    MazePrototypeFactory augments the MazeFactory interface with a constructor that takes the prototypes as arguments:

 class MazePrototypeFactory : public MazeFactory { 
    public: 
 MazePrototypeFactory(Maze*, Wall*, Room*, Door*); 
     
        virtual Maze* MakeMaze() const; 
        virtual Room* MakeRoom(int) const; 
        virtual Wall* MakeWall() const; 
        virtual Door* MakeDoor(Room*, Room*) const; 
     
    private: 
        Maze* _prototypeMaze; 
        Room* _prototypeRoom; 
        Wall* _prototypeWall; 
        Door* _prototypeDoor; 
    };


 

    // The new constructor simply initializes its prototypes: 
    MazePrototypeFactory::MazePrototypeFactory ( 
        Maze* m, Wall* w, Room* r, Door* d 
    ) { 
       _prototypeMaze = m; 
       _prototypeWall = w; 
       _prototypeRoom = r; 
       _prototypeDoor = d; 
    } 


    The member functions for creating walls, rooms, and doors are similar: Each clones a prototype and then initializes it. Here are the definitions of MakeWall and MakeDoor:

 Wall* MazePrototypeFactory::MakeWall () const { 
        return _prototypeWall->Clone(); 
    }
 
     
    Door* MazePrototypeFactory::MakeDoor (Room* r1, Room *r2) const { 
        Door* door = _prototypeDoor->Clone(); 
        door->Initialize(r1, r2); 
        return door; 
    } 


    We can use MazePrototypeFactory to create a prototypical or default maze just by initializing it with prototypes of basic maze components: 

 MazeGame game; 
 MazePrototypeFactory simpleMazeFactory( 
     new Maze, new Wall, new Room, new Door 
 ); 
    
 Maze* maze = game.CreateMaze(simpleMazeFactory); 


    An object that can be used as a prototype, such as an instance of Wall, must support the Clone operation. It must also have a copy constructor for cloning.

 class Door : public MapSite { 
    public: 
        Door(); 
        Door(const Door&); 
     
        virtual void Initialize(Room*, Room*); 
        virtual Door* Clone() const; 
     
        virtual void Enter(); 
        Room* OtherSideFrom(Room*); 
    private: 
        Room* _room1; 
        Room* _room2; 
    }; 
     
    
 Door::Door (const Door& other) { 
     _room1 = other._room1; 
     _room2 = other._room2; 
 } 

 void Door::Initialize (Room* r1, Room* r2) { 
     _room1 = r1; 
     _room2 = r2; 
 } 
     
 Door* Door::Clone () const { 
     return new Door(*this); 
 } 


 

// The BombedWall subclass must override Clone and implement a corresponding 
// copy constructor. 
 class BombedWall : public Wall { 
    public: 
        BombedWall(); 
        BombedWall(const BombedWall&); 
     
        virtual Wall* Clone() const; 
        bool HasBomb(); 
    private: 
        bool _bomb; 
    }; 
     
    BombedWall::BombedWall (const BombedWall& other) : Wall(other) { 
        _bomb = other._bomb; 
    } 
     
    Wall* BombedWall::Clone () const { 
        return new BombedWall(*this); 
    }


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值