首页 技术 正文
技术 2022年11月14日
0 收藏 944 点赞 3,599 浏览 2839 个字

声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635

桥接模式可以和排列组合关联起来理解,一个对象有多种不通种类的属性,如attributeA1,attributeA2,attributeB1,attributeB2,attributeB3。可以组装出23=6种对象。作为服务的提供方,应该怎样提供这种服务给客户端呢?是代码中写死23中组合还是交给客户端一定的灵活性,使客户端可以灵活组装出自己想要的对象呢?无论是代码的可扩展性还是从代码的简洁性来讲,我们都会选择后者。(注:这是作者第一次在设计模式讲解中引入了服务端、客户端的说法,事实上,设计模式的完成一半都要涉及服务端、客户端的说法,如果只存在服务方而客户端的配置,大部分的设计模式是没有意义的。)
以显示字体为例,不同的字体在同样的操作系统上会显示不同的样式。同样的字体在不通的操作系统上也可能显示不同的样式。那这就是很典型的可以使用Bridge模式的场景。

字体抽象类

package com.designpattern.bridge;/**
* 字体抽象类
*/
public abstract class Font {
protected FontImplOSPlatform fontImplOSPlatform; public abstract void DrawText(String text); protected FontImplOSPlatform getFontOSPlatform(String type) {
if(type.equals("Windows")) {
return new FontImplOnWindows();
} else if(type.equals("Linux")) {
return new FontImplOnLinux();
} else {
return new FontImplOnOtherOS();
}
}
}

宋体实现类

package com.designpattern.bridge;/**
* 宋体
*/
public class SongTypeFaceFont extends Font {
public SongTypeFaceFont(String osType) {
fontImplOSPlatform = getFontOSPlatform(osType);
} public void DrawText(String text) {
System.out.println("will render \"" + text + "\"");
System.out.println("The text is Song style text!");
fontImplOSPlatform.DrawTextOnOS();
}
}

楷书实现类

package com.designpattern.bridge;/**
* 楷书
*/
public class RegularStyleFont extends Font {
public RegularStyleFont(String osType) {
fontImplOSPlatform = getFontOSPlatform(osType);
} public void DrawText(String text) {
System.out.println("will render \"" + text + "\"");
System.out.println("The text is regular style text!");
fontImplOSPlatform.DrawTextOnOS();
}
}

操作系统字体实现接口类

package com.designpattern.bridge;public interface FontImplOSPlatform {
void DrawTextOnOS();
}

Windows实现字体渲染

package com.designpattern.bridge;/**
* 字体在Windows上的实现
*/
public class FontImplOnWindows implements FontImplOSPlatform {
public void DrawTextOnOS() {
System.out.println("The text will be rendered on Windows");
}
}

Linux实现字体渲染

package com.designpattern.bridge;/**
* 字体在Linux上的实现
*/
public class FontImplOnLinux implements FontImplOSPlatform {
public void DrawTextOnOS() {
System.out.println("The text will be rendered on Linux");
}
}

其他操作系统实现字体渲染

package com.designpattern.bridge;/**
* 字体在其他操作系统上的实现
*/
public class FontImplOnOtherOS implements FontImplOSPlatform {
public void DrawTextOnOS() {
System.out.println("The text will be rendered on Other OS");
}
}

测试类(客户端类)

   package com.designpattern.bridge;public class TestBridge {
public static void main(String[] args) {
Font myText = new SongTypeFaceFont("Windows");
myText.DrawText("中国");
System.out.println(); myText = new SongTypeFaceFont("Linux");
myText.DrawText("中国");
System.out.println(); myText = new RegularStyleFont("Windows");
myText.DrawText("中国");
System.out.println(); myText = new RegularStyleFont("Linux");
myText.DrawText("中国");
System.out.println();
}
}

运行结果如下

will render "中国"
The text is Song style text!
The text will be rendered on Windowswill render "中国"
The text is Song style text!
The text will be rendered on Linuxwill render "中国"
The text is regular style text!
The text will be rendered on Windowswill render "中国"
The text is regular style text!
The text will be rendered on Linux

完。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291