Bridge模式
在学习Bridge
模式的时候,一直想不通这个模式的具体应用场景,不知道为什么要使用这个模式,直到今天又重新看了一遍《图解设计模式》课后的讲解加上自己的思考,才对这个模式有了更深的理解。
假设我们有这样一个场景,我们希望实现某种打印字符串的功能,这个功能在Windows
,Mac
和Linux
上都可以使用,那么最简单的实现方案是我们实现三个类WindowsPrint
,MacPrint
和LinuxPrint
。但是,由于我们希望暴露给上层调用接口是统一的,也就是接口并不需要知道具体是在哪种平台Print
,而只需要调用Print
就好了。
依据我们之前学过的OOP的想法,是可以首先定义一个接口或者基类,这个基类声明了我们打印所需要的方法,然后具体的实现在子类中编写。这样看似解决了问题,并且在很多时候我们都是这样做的。
但是现在,假设我们在接口或者基类中增加了一个方法,这个时候,我们使用继承的这种方法,就显得很不灵活。我们需要对这个接口进行修改,而这就违背了我们之前的Open for extension but close for modification
的原则。
这时候,我们就可以很好地使用桥接模式来完成我们的设计。桥接模式最重要的特征是将类的功能层次结构与类的实现层次结构分离开,使得我们可以很好的对他们进行扩展。
我们通过一个小例子来看桥接模式,我们希望有一个接口可以在不同的平台上打印信息:
public class Print {
private PrintImpl printString;
public Print(PrintImpl printString) {
this.printString = printString;
}
public void open(){
this.printString.rawOpen();
}
public void print(){
this.printString.rawPrint();
}
public void close(){
this.printString.rawClose();
}
public void display(){
open();
print();
close();
}
}
可以看到,我们并没有直接在Print
类中编写函数,也没有将Print
声明为接口或父类让其他函数继承,而是通过委托的方式,使用了Print
类中的PrintImpl
成员变量的相应函数。
public interface PrintImpl {
public abstract void rawOpen();
public abstract void rawPrint();
public abstract void rawClose();
}
在Windows
和Linux
的具体实现:
public class WindowsPrint implements PrintImpl {
private String string;
public WindowsPrint(String string) {
this.string = string;
}
@Override
public void rawOpen() {
System.out.println("##### Windows #####");
System.out.print("+");
for (int i = 0 ; i < string.length();i++){
System.out.print("-");
}
System.out.println("+");
}
@Override
public void rawPrint() {
System.out.println(String.format("|%s|", string));
}
@Override
public void rawClose() {
System.out.print("+");
for (int i = 0 ; i < string.length();i++){
System.out.print("-");
}
System.out.println("+");
}
}
public class LinuxPrint implements PrintImpl{
private String string;
public LinuxPrint(String string) {
this.string = string;
}
@Override
public void rawOpen() {
System.out.println("%%%%% Linux %%%%%");
System.out.print("+");
for (int i = 0 ; i < string.length();i++){
System.out.print("-");
}
System.out.println("+");
}
@Override
public void rawPrint() {
System.out.println(String.format("*%s*", string));
}
@Override
public void rawClose() {
System.out.print("+");
for (int i = 0 ; i < string.length();i++){
System.out.print("-");
}
System.out.println("+");
}
}
Main.java
public class Main {
public static void main(String args []){
Print p = new Print(new WindowsPrint("Hello,World"));
p.display();
Print p2 = new Print(new LinuxPrint("Hello,World"));
p2.display();
}
}
##### Windows #####
±----------+
|Hello,World|
±----------+
%%%%% Linux %%%%%
±----------+
*Hello,World*
±----------+Process finished with exit code 0
如果我们需要增加一个新的打印功能,按照以往的思路,我们在Print
中声明新的函数,然后在具体的PrintImpl
中来实现具体的函数。很多时候,继承的方式就是这样来实现的。
在桥接模式中,我们将类的功能层次结构和类的实现结构层次分离开,我们希望两者独立地扩展。因此,我们只需要创建一个新的MultiPrint
类,这个类继承自Print
并扩展一个新的函数。
public class MultiPrint extends Print{
public MultiPrint(PrintImpl printString) {
super(printString);
}
public void multiPrint(int cnt){
open();
for(int i = 0 ; i <cnt;i++){
print();
}
close();
}
}
同时,在Main
函数中我们这样调用:
MultiPrint p3 = new MultiPrint(new WindowsPrint("Hello,World"));
p3.multiPrint(10);
##### Windows #####
±----------+
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
|Hello,World|
±----------+Process finished with exit code 0
一个一般的Bridge
模式UML
类图如下:
桥接模式的核心就是,在我们进行类设计的时候,我们将类的功能结构层次和类的实现结构层次分开,并且以委托的方式,委托Implementor
进行功能的实现。当我们需要增加新的功能时,我们只需要在类的结构层次,继承原有接口,增加新的类即可。
在设计模式的学习中,我们发现很多设计模式最终的目的就是为了解耦,将功能与实现分离开来,使得两者可以独立地扩展。同时,Bridge模式
很好的践行了开闭原则,Open for extension but close for modification
。