题目:如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如 Windows、Linux、Unix等)上播放多种格式的视频文件,如MPEG、RMVB、AVI、WMV等常见视频格式。现使用桥接模式设计该播放器。
类图
代码
package 桥接模式实例之跨平台视频播放器;
public class AVIFile implements VideoFile {
public void decode(String osType, String fileName) {
System.out.println("操作系统:"+osType+"文件名称:"+fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public class WMVFile implements VideoFile {
public void decode(String osType, String fileName) {
System.out.println("操作系统:"+osType+"文件名称:"+fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public class MPEGFile implements VideoFile {
public void decode(String osType, String fileName) {
System.out.println("操作系统:"+osType+"文件名称:"+fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public class RMVBFile implements VideoFile {
public void decode(String osType, String fileName) {
System.out.println("操作系统:"+osType+"文件名称:"+fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public interface VideoFile {
public void decode(String osType, String fileName);
}
package 桥接模式实例之跨平台视频播放器;
public abstract class OperatingSystemVersion {
protected VideoFile vf;
public void setVideo(VideoFile vf) {
this.vf = vf;
}
public abstract void play(String fileName);
}
package 桥接模式实例之跨平台视频播放器;
public class LinuxVersion extends OperatingSystemVersion {
public void play(String fileName) {
String osType = "Linux播放";
this.vf.decode(osType,fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public class UnixVersion extends OperatingSystemVersion {
public void play(String fileName) {
String osType = "Unix播放";
this.vf.decode(osType,fileName);
}
}package 桥接模式实例之跨平台视频播放器;
public class WindowsVersion extends OperatingSystemVersion {
public void play(String fileName) {
String osType = "Windows播放";
this.vf.decode(osType,fileName);
}
}
package 桥接模式实例之跨平台视频播放器;
public class Client {
public static void main(String args[]) {
VideoFile vf;
OperatingSystemVersion osType1 = new WindowsVersion();
vf = new AVIFile();
osType1.setVideo(vf);
osType1.play("AVI");
OperatingSystemVersion osType2 = new LinuxVersion();
vf = new AVIFile();
osType2.setVideo(vf);
osType2.play("AVI");
OperatingSystemVersion osType3 = new UnixVersion();
vf = new AVIFile();
osType3.setVideo(vf);
osType3.play("AVI");
}
}
运行效果图
本文介绍了如何使用桥接模式设计一个跨平台的视频播放器,该播放器能够在Windows、Linux、Unix等多个操作系统上播放MPEG、RMVB、AVI、WMV等多种格式的视频文件。通过类图和代码示例详细阐述了桥接模式在实现中的作用。
1722

被折叠的 条评论
为什么被折叠?



