结构型模式之桥接模式(跨平台视频播放器)

来源:互联网 发布:数据库监控 开源 编辑:程序博客网 时间:2024/06/13 09:46

题目:如果需要开发一个跨平台视频播放器,可以在不同操作系统平台(如 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");}}
运行效果图


0 0
原创粉丝点击