目录
3、工厂实现(其它产品实现,由于篇幅关系,此不写出,上一次报告中已有) 4
一、要求
应用工厂方法,实现前面的画圆操作。
二、工厂方法思想:
通过定义一个用于创建对象的接口,让子类决定实例化哪一个类,使一个类的实例化延迟到其子类。
三、具体程序体现:1、定义产品及其分支
class TShape
{
public:
virtual void Draw(CDC *pDC)=0;
virtual bool Intersect(CPoint aPoint)=0;
virtual double GetArea(void)=0;
virtual void SetName(const CString Value) {FName=Value;}
virtual CString GetName() {return FName;}
private:
CString FName;
};
class TCircle:public TShape
{
public:
TCircle();
TCircle(CPoint aCenter,int aRadius);
~TCircle();
void Draw(CDC *pDC);
double GetArea(void);
bool Intersect(CPoint aPoint);
void SetName(const CString Value);
CString GetName();
private:
CPoint FCenter;
int FRadius;
CString FName;
};
class TRectangle:public TShape
{
public:
TRectangle();
TRectangle(CPoint aLeftTop,CPoint aRightBottom);
~TRectangle() {}
void Draw(CDC *pDC);
double GetArea(void);
bool Intersect(CPoint aPoint);
void SetName(const CString Value);
CString GetName();
private:
CPoint FLeftTop;
CPoint FRightBottom;
CString FName;
};
2、定义工厂及其分支
class fName{
public:
fName() {}
virtual CString GetName() {return MyName;}
virtual void SetName(CString me) {MyName=me;}
public:
CString MyName;
};
class CircleName:public fName{
public:
CircleName(){}
CString GetName() {return MyName;}
void SetName(CString me) {MyName=me;}
};
class RectangleName:public fName{
public:
RectangleName() {}
CString GetName() {return MyName;}
void SetName(CString me) {MyName=me;}
};
class Point{
public:
Point() {}
virtual void SetPoint(CPoint) {}
virtual void SetR(int) {}
virtual CPoint GetPoint() {return CPoint(0,0);}
virtual int GetR() {return 0;}
virtual void SetLTPoint(CPoint) {}
virtual void SetRBPoint(CPoint) {}
virtual CPoint GetLTPoint() {return CPoint(0,0);}
virtual CPoint GetRBPoint() {return CPoint(0,0);}
};
class CirPoint:public Point{
public:
CirPoint() {}
void SetPoint(CPoint cPoint) {aPoint=cPoint;}
void SetR(int Ra) {r=Ra;}
CPoint GetPoint() {return aPoint;}
int GetR() {return r;}
private:
CPoint aPoint;
int r;
};
class RectPoint:public Point{
public:
RectPoint() {}
void SetLTPoint(CPoint oPoint) {LTPoint=oPoint;}
void SetRBPoint(CPoint sPoint) {RBPoint=sPoint;}
CPoint GetLTPoint() {return LTPoint;}
CPoint GetRBPoint() {return RBPoint;}
private:
CPoint LTPoint;
CPoint RBPoint;
};
class FactoryPic{
public:
FactoryPic() {}
virtual fName* MakeName()=0;
virtual Point* MakePoint()=0;
virtual TShape* CreatePicByMethod()=0;
};
class FactoryCircle:public FactoryPic{
public:
FactoryCircle() {}
fName* MakeName();
Point* MakePoint();
TShape* CreatePicByMethod();
};
class FactoryTRectangle:public FactoryPic{
public:
FactoryTRectangle() {}
fName* MakeName();
Point* MakePoint();
TShape* CreatePicByMethod();
};
3、工厂实现
fName* FactoryCircle::MakeName(){
char buffer[20];
_itoa(rand()%200,buffer,10);
CString TName=(CString)"我的名字是Circle"+buffer+";";
fName *m=new CircleName;
m->SetName(TName);
return m;
}
Point* FactoryCircle::MakePoint(){
int r=10+rand()%100;
CPoint mPoint(rand()%480,rand()%300);
Point *CirP=new CirPoint;
CirP->SetR(r);
CirP->SetPoint(mPoint);
return CirP;
}
TShape* FactoryCircle::CreatePicByMethod(){
fName* n;
Point* p;
n=MakeName();
p=MakePoint();
TShape *hello=new TCircle(p->GetPoint(),p->GetR());
hello->SetName(n->GetName());
return hello;
}
fName* FactoryTRectangle::MakeName(){
char buffer[20];
_itoa(rand()%200,buffer,10);
CString TName=(CString)"我的名字是TRectangle"+buffer+";";
fName *m=new RectangleName;
m->SetName(TName);
return m;
}
Point* FactoryTRectangle::MakePoint(){
int a,b,offset,offset1;
a=rand()%450;
b=rand()%250;
offset=rand()%100+20;
offset1=offset+rand()%100+10;
CPoint oPoint(a,b),sPoint(a+offset1,b+offset);
Point* mPoint=new RectPoint;
mPoint->SetLTPoint(oPoint);
mPoint->SetRBPoint(sPoint);
return mPoint;
}
TShape* FactoryTRectangle::CreatePicByMethod(){
fName* n;
Point *op,*sp;
n=MakeName();
sp=MakePoint();
TShape *hello=new TRectangle(sp->GetLTPoint(),sp->GetRBPoint());
hello->SetName(n->GetName());
return hello;
}
4、按钮相关
void CCircleTestDlg::OnCircle()
{
// TODO: Add your control notification handler code here
TShape *AShape;
FactoryPic *MyCircle=new FactoryCircle;
AShape=MyCircle->CreatePicByMethod();
CDC *pDC= new CClientDC(this);
AShape->Draw(pDC);
List.Add(AShape);
}
void CCircleTestDlg::OnTRectangle()
{
// TODO: Add your control notification handler code here
TShape *AShape;
FactoryPic *MyRectangle=new FactoryTRectangle;
AShape=MyRectangle->CreatePicByMethod();
CDC *pDC= new CClientDC(this);
AShape->Draw(pDC);
List.Add(AShape);
}