public class SmartEditor implements Editor { private static Editor textEditor = new TextEditor(); private static Editor xmlEditor = new XMLEditor(); public void openFile(String path) { if (path.endsWith(".xml")) { xmlEditor.openFile(path); } else { textEditor.openFile(path); } } }
小时候我们都用蜡笔画画,一盒蜡笔12种颜色。一开始我都是用最小号的蜡笔画个太阳公公、月亮婆婆足够了。后来开始画一些抽象派的作品,就得换中号的了,要不然画个背景都要描半天,好一盒中号的也是12种颜色。再后来我开始转向豪放派,中号就有些捉襟见肘了,只好换大号的了,好一盒大号的也只有12种颜色。你看,像我这样不太出名的画家就需要36种画笔,哇,太麻烦了。但是据我观察,另一些比我出名的画家倒是没有这么多笔,他们只有几把刷子和一些颜料,这样就解决了蜡笔的“种类爆炸”问题。
这是在学习Bridge模式时我们经常会举的例子。12*3与12+3的区别让我们认识这种模式的强大。
Editor接口
package com.gemplus.editor; public abstract interface Editor { public void openFile(String path); }
TextEditor实现
package com.gemplus.editor; public class TextEditor implements Editor { public void openFile(String path) { System.out.println("Open file with Text Editor. FileName: " + path); } }
XMLEditor实现
package com.gemplus.editor; public class XMLEditor implements Editor { public void openFile(String path) { System.out.println("Open file with XML Editor. FileName: " + path); } }
Client调用
package com.gemplus.editor; public class Client { public static void main(String[] args) { Editor xmlEditor = new XMLEditor(); xmlEditor.openFile("test.xml"); Editor textEditor = new TextEditor(); textEditor.openFile("test.txt"); } }
到目前为止,我们什么模式也没用到。不过我们还是用到了一项伟大的技术多态,还有就是对接口编程。代码写的还算优雅,只是没有写注释。
接下来我们要增加一个功能,与其说是功能,不如说是一项约束。就是,我不希望客户端了解那些文件要由xmlEditor打开,哪些由textEditor打开。但是总要有人知道,对吧?这项看似直观的约束往往被忽略,或者在潜意识中没有意识到。为了简单起见,我们引入一个简单工厂EditorImpl。
Client也要做相应修改
public class Client { public static void main(String[] args) { Editable editableText = new FileEditable(new TextEditor(), "test.txt"); Editable editableXML = new FileEditable(new XMLEditor(), "test.xml"); editableText.open(); editableXML.open(); } }
输出仍然没有变化。(前面做的其实也可以算作是重构吧,可观察行为没有变化)不得不承认,现在客户端又需要了解什么样的文件用什么编辑器打开了。但是一个小的变化就可以避免这一点。
public FileEditable(String filePath) { this.filePath = filePath; if (filePath.endsWith(".xml")) { editor = new XMLEditor(); } else { editor = new TextEditor(); } }
现在该是增加功能的时候了。现在我们要求,用户不但可以打开文件也有可能用这个Editor来编辑一段文字,即字符串。我们假设,所有的Editor都可以编辑文件和字符串。我们为Editor增加一个方法:openString。前面我们说了,Bridge模式中有两个继承体系,两边可以独立变化。现在我们有了两个Editable,即两种可编辑体:文件和字符串。也可以看出我们当初把Editable封装起来多么的英明神武啊!!
在Editable的继承体系中又增加了一员:StringEditable。
虽然我前面已经说了,Abstraction和Implementor的聚合关系可以由派生类RefinedAbstraction和Implementor的聚合关系代替,为了与经典模式类图保持尽量的相似,以便大家容易理解,我们还是在StringEditable和FileEditable上面加了一层:EditableImpl。
输出
Open file with XMLEditor. FileName: test.xml
Open file with Text Editor. FileName: test.txt