找考题网-背景图
填空题


阅读以下说明和C++代码,
[说明]
现要编写一个画矩形的程序,目前有两个画图程序:DP1和DP2,DP1用函数draw_a_line(x1,y1,x2,y2)画一条直线,DP2则用drawline(x1,x2,y1,y2)画一条直线。当实例化矩形时,确定使用DP1还是DP2。为了适应变化,包括“不同类型的形状”和“不同类型的画图程序”,将抽象部分与实现部分分离,使它们可以独立地变化。这里,“抽象部分”对应“形状”,“实现部分”对应“画图”,与一般的接口(抽象方法)与具体实现不同。这种应用称为Bridge(桥接)模式。图6-1显示了各个类间的关系。
[图6-1]

这样,系统始终只处理3个对象:Shape对象、Drawingg对象、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{
privatc:
(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{
privatc:
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=yl;_x2=x2;_y2=y2;
}
void Rectangle::draw()
{
//省略具体实现
}

【参考答案】

Shape(dp)
热门试题

填空题Drawing

填空题exchange(dr,m,b)

填空题super(dp)