声明:迁移自本人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 Windows
will render "中国"
The text is Song style text!
The text will be rendered on Linux
will render "中国"
The text is regular style text!
The text will be rendered on Windows
will render "中国"
The text is regular style text!
The text will be rendered on Linux
完。