Shape * ashape =newCircle();
classMainForm :publicForm
{
ShapeFactory* factory;//工厂public:
MainForm(ShapeFactory*factory)
{this->factory =factory;
}virtual~MainForm();voidMainCreate()
{//多态newShape * ashape =factory->CreateShape();//获取Noashape->GetNo();
}
};
classShape
{public:
Shape(intno =0) :no_(){}virtual~Shape(){}intGetNo(){returnno_; }intSetNo(intno_){}private:intno_;
};
classShapeFactory
{public:
ShapeFactory();virtualShape * CreateShape() =0;virtual~ShapeFactory(){}
};
classLine :publicShape
{public:
Line(){}virtual~Line(){}
};classRec :publicShape
{public:
Rec(){}virtual~Rec(){}
};classCircle :publicShape
{public:
Circle(){}virtual~Circle(){}
};
classLineFactory :publicShapeFactory
{public:
LineFactory();virtual~LineFactory();virtualShape*CreateShape()
{returnnewLine();
}
};classRecFactory :publicShapeFactory
{public:
RecFactory();virtual~RecFactory();virtualShape*CreateShape()
{returnnewRec();
}
};classCircleFactory :publicShapeFactory
{public:
CircleFactory();virtual~CircleFactory();virtualShape*CreateShape()
{returnnewCircle();
}
};
网友评论