class BombedWall : public Wall {
public:
BombedWall();
BombedWall(const BombedWall&);
virtual Wall* Clone() const;
bool HasBomb();
};
BombedWall::BombedWall (const BombedWall& other) : Wall(other)
{
_bomb = other._bomb;
}
Wall* BombedWall::Clone() const { // 虽然BombedWall::Clone返回一个Wall*, 但是它的实现返回了一个指向子类的新实例的指针,即BombedWall*.
return new BombedWall( *this );
}
相关模式 Prototype和Abstract Factory模式在某种方面是相互竞争的。但是它们也可以一起使用,Abstract Factory可以存储一个被克隆的原型的集合,并且返回产品对象。大量使用Composite和Decorator模式的设计通常也可以从Prototype模式处获益。