外观模式
定义:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易实现
UML图
外观模式应用实例
SubSystems Class
public class SubSystemOne { public void methodOne(){ System.out.println("subsystem one run...."); } }
public class SubSystemTwo { public void methodTwo(){ System.out.println("subsystem two run...."); } }
public class SubSystemThree { public void methodthree(){ System.out.println("subsystem three run...."); } }
Facade Class
public class Facade { private SubSystemOne one; private SubSystemTwo two; private SubSystemThree three; public Facade() { this.one = new SubSystemOne(); this.two = new SubSystemTwo(); this.three = new SubSystemThree(); } public void methodA(){ System.out.println("第一种组合方式"); one.methodOne(); three.methodthree(); } public void methodB(){ System.out.println("第二种组合方式"); one.methodOne(); two.methodTwo(); three.methodthree(); } }
Client Class
public class Client { public static void main(String[] args) { Facade facade=new Facade(); //调用具体的方法即可 facade.methodA(); facade.methodB(); } }