外观模式
外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。

外观模式结构图
2.5.x 案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| public class Stock1 { public void sell(){ System.out.println(" 股票1卖出"); } public void buy(){ System.out.println(" 股票1买入"); } }
......
public class Realty1 { public void sell(){ System.out.println(" 不动产1卖出"); } public void buy(){ System.out.println(" 不动产1买入"); } }
public class NationalDebt1 { public void sell(){ System.out.println(" 国债1卖出"); } public void buy(){ System.out.println(" 国债1买入"); } }
public class Test { public static void main(String[] args) { Stock1 gu1 = new Stock1(); Stock2 gu2 = new Stock2(); Stock3 gu3 = new Stock3(); NationalDebt1 nd1 = new NationalDebt1(); Realty1 rt1 = new Realty1();
gu1.buy(); gu2.buy(); gu3.buy(); nd1.buy(); rt1.buy();
gu1.sell(); gu2.sell(); gu3.sell(); nd1.sell(); rt1.sell(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class Fund { Stock1 gu1; Stock2 gu2; Stock3 gu3; NationalDebt1 nd1; Realty1 rt1;
public Fund() { this.gu1 = new Stock1(); this.gu2 = new Stock2(); this.gu3 = new Stock3(); this.nd1 = new NationalDebt1(); this.rt1 = new Realty1(); }
public void buyFund(){ gu1.buy(); gu2.buy(); gu3.buy(); nd1.buy(); rt1.buy(); }
public void sellFund(){ gu1.sell(); gu2.sell(); gu3.sell(); nd1.sell(); rt1.sell(); } } public static void main(String[] args) { Fund jijin = new Fund(); jijin.buyFund(); jijin.sellFund(); }
|
2.5.x 代码结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class SubSystemOne { public void methodOne(){ System.out.println(" 子系统方法一"); } }
public class SubSystemTwo { public void methodTwo(){ System.out.println(" 子系统方法二"); } }
public class SubSystemThree { public void methodThree(){ System.out.println(" 子系统方法三"); } }
public class SubSystemFour { public void methodFour(){ System.out.println(" 子系统方法四"); } }
public class Facade { SubSystemOne one; SubSystemTwo two; SubSystemThree three; SubSystemFour four;
public Facade() { this.one = new SubSystemOne(); this.two = new SubSystemTwo(); this.three = new SubSystemThree(); this.four = new SubSystemFour(); }
public void methodA(){ System.out.println("\n方法组A() --- "); one.methodOne(); two.methodTwo(); four.methodFour(); }
public void methodB(){ System.out.println("\n方法组B() --- "); two.methodTwo(); three.methodThree(); } }
public class Main { public static void main(String[] args) { Facade facade = new Facade(); facade.methodA(); facade.methodB(); } }
|
外观模式的使用场景
三个阶段,首先在设计初期阶段,应该要有意识将不同的两个层分离,如三层架构需要考虑在数据访问层和业务逻辑层、业务逻辑层和表示层的层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合大大降低。
其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也都会产生很多很小的类,这本应是好事,但也给外部调用它们的用户程序带来了使用上的困难,增加外观Facade可以提供一个简单的接口,减少它们之间的依赖。
第三,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但因为它包含非常重要的功能,新的需求开发必须要依赖于它。此时使用外观模式Facade也是非常合适的。你可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰简单的接口,让新系统与Facademic对象交互,Facade与遗留代码交互所有复杂的工作。

外观模式使用场景
参考:
🔗 《设计模式:可复用面向对象软件的基础》
🔗 《大话设计模式》