|
翻译TIPatterns--降低接口复杂度(Reducing interface complexity)(1)
降低接口复杂度(RedUCing interface complexity)
有时候你需要解决的是很简单的问题,比如“当前的接口不是你正好需要的”。Façade模式(外观)通过为库或者一堆资源提供一个更易用的使用方法,为一系列类创建一个接口。 外观Façade 当我想方设法试图将需求初步(first-cut)转化成对象的时候,通常我使用的原则是:“把所有丑陋的东西都隐藏到对象里去”。基本上说,Façade干的就是这个事情。如果你有一堆让人头晕的类以及Interactions? ,而它们又不是客户端程序员必须了解的,那你就可以为客户端程序员创建一个接口只提供那些必要的功能。 Façade模式经常被实现为一个符合singleton模式的抽象工厂(abstract factory)。当然,你可以通过创建包含静态工厂方法(static factory methods)的类来达到上述效果。 //: facade:Facade.Java package facade; import junit.framework.*; class A { public A(int x) {} } class B { public B(long x) {} } class C { public C(double x) {} } // Other classes that aren't eXPosed // by the facade go here ... public class Facade extends TestCase { static A makeA(int x) { return new A(x); } static B makeB(long x) { return new B(x); } static C makeC(double x) { return new C(x); } public void test() { // The client programmer gets the objects // by calling the static methods: A a = Facade.makeA(1); B b = Facade.makeB(1); C c = Facade.makeC(1.0); } public static void main(String args[]) { junit.textui.TestRunner.run(Facade.class); } } ///:~ 《设计模式》给出的例子是通过一个类使用另外一个类(来实现façade模式的)。
|