阅读以下说明和c++代码,将应填入(n)处的字句写在对应栏内。【说明】现要编写一个画矩形的程序,目前有两个画图程序:DP1和DP2,DP1用函数draw_a_line(x1, y1,x2,y2)画一条直线,DF2则用drawline(x1,x2,y1,y2)画一条直线。当实例画矩形时,确定使用DP1还是DP2。为了适应变化,包括“不同类型的形状”和“不同类型的画图程序”,将抽象部分与实现部分分离,使它们可以独立地变化。这里,“抽象部分”对应“形状”,“实现 部分”对应“画图”,与一般的接口(抽象方法)与具体实现不同。这种应用称为Bridge(桥接)模式。图9-7显示了各个类间的关系。这样,系统始终只处理3个对象:Shape对象、Drawing对象、DP1或DP2对象。以下是 C++语言实现,能够正确编译通过。【C++代码】class DP1{public:static void draw_a_line(double x1, double y1,double x2, double y2){//省略具体实现});class DP2{public:static void drawline(double x1, double x2,double y1, double y2){//省略具体实现}};class Drawing{public:(1) void drawLine(double x1,double y1,double x2,double y2)=0;};class V1Drawing:public Drawing{public:void drawLine(double x1, double y1,double x2, double y2){DP1::draw_a_line(x1,y1,x2,y2);}};class V2Drawing:public Drawing{public:void drawLine(double x1, double y1, double x2, double y2){(2);}};class Shape{private:(3) _dp;public:Shape(Drawing *dp);virtual void draw()=0;void drawLine(double x1, double y1, double x2, double y2);};Shape::Shape(Drawing *dp){_dp = dp;}void Shape::drawLine(double x1, double y1, double x2, double y2){ //画一条直线(4);}class Rectangle: public Shape{private:double _x1,_y1,_x2,_y2;public:Rectangle(Drawing *dp, double x1, double y1,double x2, double y2);void draw();};Rectangle::Rectangle(Drawing *dp, double x1, double y1, double x2, double y2):(5){_x1=x1;_y1=y1;_x2=x2;_y2=y2;}void Rectangle::draw(){//省略具体实现}
阅读以下说明和c++代码,将应填入(n)处的字句写在对应栏内。
【说明】
现要编写一个画矩形的程序,目前有两个画图程序:DP1和DP2,DP1用函数draw_a_line(x1, y1,x2,y2)画一条直线,DF2则用drawline(x1,x2,y1,y2)画一条直线。当实例画矩形时,确定使用DP1还是DP2。为了适应变化,包括“不同类型的形状”和“不同类型的画图程序”,将抽象部分与实现部分分离,使它们可以独立地变化。这里,“抽象部分”对应“形状”,“实现 部分”对应“画图”,与一般的接口(抽象方法)与具体实现不同。这种应用称为Bridge(桥接)模式。图9-7显示了各个类间的关系。
这样,系统始终只处理3个对象:Shape对象、Drawing对象、DP1或DP2对象。以下是 C++语言实现,能够正确编译通过。
【C++代码】
class DP1{
public:
static void draw_a_line(double x1, double y1,double x2, double y2){
//省略具体实现
}
);
class DP2{
public:
static void drawline(double x1, double x2,double y1, double y2){
//省略具体实现
}
};
class Drawing{
public:
(1) void drawLine(double x1,double y1,double x2,double y2)=0;
};
class V1Drawing:public Drawing{
public:
void drawLine(double x1, double y1,double x2, double y2){
DP1::draw_a_line(x1,y1,x2,y2);
}
};
class V2Drawing:public Drawing{
public:
void drawLine(double x1, double y1, double x2, double y2){
(2);
}
};
class Shape{
private:
(3) _dp;
public:
Shape(Drawing *dp);
virtual void draw()=0;
void drawLine(double x1, double y1, double x2, double y2);
};
Shape::Shape(Drawing *dp)
{
_dp = dp;
}
void Shape::drawLine(double x1, double y1, double x2, double y2)
{ //画一条直线
(4);
}
class Rectangle: public Shape{
private:
double _x1,_y1,_x2,_y2;
public:
Rectangle(Drawing *dp, double x1, double y1,
double x2, double y2);
void draw();
};
Rectangle::Rectangle(Drawing *dp, double x1, double y1, double x2, double y2)
:(5)
{
_x1=x1;_y1=y1;_x2=x2;_y2=y2;
}
void Rectangle::draw()
{
//省略具体实现
}