P.S. 这周居然日平均上班时间达到了19h。。。已跪,救命
用的工厂方法
class DrawSys
{
private:
Shape shape;
public:
void draw() {/*...*/ }
};
class Shape
{
private:
ShapeFactory* shape;
public:
Shape(ShapeFactory* shapefac)
:shape(shapefac)
{}
~Shape()
{
delete shape;
}
void Draw()
{
IShape* ishape = shape->CreateShape();
ishape->Drawing();
delete ishape;
}
};
class IShape
{
public:
virtual void Drawing() = 0;
virtual ~IShape() {}
};
class Line :public IShape
{
public:
virtual void Drawing() {/*...*/ }
};
class Rec :public IShape
{
public:
virtual void Drawing() {/*...*/ }
};
class Circle :public IShape
{
public:
virtual void Drawing() {/*...*/}
};
class ShapeFactory
{
public:
virtual IShape* CreateShape() = 0;
virtual ~ShapeFactory() {}
};
class LineFactory : public ShapeFactory
{
public:
IShape* CreateShape()
{
return new Line();
}
};
class RecFactory : public ShapeFactory
{
public:
IShape* CreateShape()
{
return new Rec();
}
};
class CircleFactory : public ShapeFactory
{
public:
IShape* CreateShape()
{
return new Circle();
}
};