某图像预览程序要求能够查看BMP、JPEG和GIF三种格式的文件,且能够在Windows和Linux两种操作系统上运行。程序需具有较好的扩展性以支持新的文件格式和操作系统。为满足上述需求并减少所需生成的子类数目,现采用桥接(Bridge)模式进行设计,得到如图6-1所示的类图。
图6-1
【Java代码】
import java.util.*;
class Matrix{//各种格式的文件最终都被转化为像素矩阵
//此处代码省略
};
abstract class Implementor{
public(1);//显示像素矩阵m
};
class WinImp extends Implementor{
public void doPaint(Matrix m){//调用Windows系统的绘制函数绘制像素矩阵
}
};
class LinuxImp extends Implementor{
public void doPaint(Matrix m){//调用Linux系统的绘制函数绘制像素矩阵
}
};
abstract class Image{
public void setImp(Implementor imp){this.imp=imp;}
public abstract void parseFile(String fileName);
protected Implementor imp;
};
class BMPImage extends Image{
//此处代码省略
};
class GIFImage extends Image{
public void parseFile(String fileName){
//此处解析BMP文件并获得一个像素矩阵对象m
(2);//显示像素矩阵m
}
};
class JPEGImage extends Image{
//此处代码省略
};
class Main{
public static void main(String[]args){
//在Linux操作系统上查看demo.gif图像文件
Image image=(3);
Implementor imageImp=(4);
(5);
Image.parseFile("demo.gif");
}
}
正确答案及解析
正确答案
解析
1.abstract void doPaint(Matrix m)
2.imp.doPaint(m)
3.new GIFImage()
4.new LinuxImp()
5.image.setImp(imageImp)
第一空是显示像素矩阵m
从类图来看Implementor是WinImp和LinuxImp两子类的父类。那就需要从子类中去找共同的方法,然后把它们抽象出来。
共同的方法为:void doPaint(Matrix m);抽象就成了abstract void doPaint(Matrix m);此处别忘了abstract关键字。是抽象方法。
第二空是显示像素矩阵m
在Image的类和其子类中,要显示像素矩阵,可以使用调用Implementor类的方法doPaint,而Image类中定义了对象imp。
即调用的方法为:imp.doPaint(m)
第三空是构造出Gif图像的对象new GIFImage()
第四空是要在Linux操作系统上查看,需要一个LinuxImp的对象.new LinuxImp()
第五空是把imageImp对象传递,以便能够查看Gif图像文件,image.setImp(imageImp)
包含此试题的试卷
你可能感兴趣的试题
Advancements in ( )have contributed to the growth of the automotive industry through the creation and evolution of self-driving vehicles.
-
- A.Artificial Intelligence
- B.Cloud Computing
- C.Internet of Things
- D.Big Data
- 查看答案
In project human resource management , ( )is not a source of power for the project manager.
-
- A.referent power
- B.expert power
- C.reward power
- D.audit power
- 查看答案
At the project establishment stage , the feasibility study mainly includes techinical feasibility analysis , ( ), operation environment feasibility analysis and other aspects of feasibility analysis.
-
- A.detail feasibility analysis
- B.opportunity analysis
- C.economic feasibility analysis
- D.risk analysis
- 查看答案
( )is a grid that shows the project resources assigned to each work package.
-
- A.Stakeholder engagement assessment matrix
- B.Requirements traceability matrix
- C.Probability and impact matrix
- D.Responsibility assignment matrix
- 查看答案
Xinhua News Agency reported in January 2022,Chian will further promote the developmet of a digital economy during the 14th Five-Year Plan eriod(2021-2025). The plan also emphasized industrial ( )transformation.
-
- A.digital
- B.networking
- C.intelligentize
- D.informatization
- 查看答案