外观模式(Facade),为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
子系统:
1 package cn.happy.design_pattern._08facade; 2 3 public class SubSystemOne { 4 5 public void MethodOne(){ 6 System.out.println("子系统方法一"); 7 } 8 } 9 public class SubSystemTwo { 10 11 public void MethodTwo(){ 12 System.out.println("子系统方法二"); 13 } 14 } 15 public class SubSystemThree { 16 17 public void MethodTree(){ 18 System.out.println("子系统方法三"); 19 } 20 } 21 public class SubSystemFour { 22 23 public void MethodFour(){ 24 System.out.println("子系统方法四"); 25 } 26 }
外观类:
1 package cn.happy.design_pattern._08facade; 2 3 public class Facade { 4 5 SubSystemOne one; 6 SubSystemTwo two; 7 SubSystemThree three; 8 SubSystemFour four; 9 public Facade() { 10 one = new SubSystemOne(); 11 two = new SubSystemTwo(); 12 three = new SubSystemThree(); 13 four = new SubSystemFour(); 14 } 15 public void MethodA(){ 16 System.out.println("方法组A:"); 17 one.MethodOne(); 18 two.MethodTwo(); 19 four.MethodFour(); 20 } 21 public void MethodB(){ 22 System.out.println("方法组B:"); 23 two.MethodTwo(); 24 three.MethodTree(); 25 } 26 }
测试类:
1 package cn.happy.design_pattern._08facade; 2 3 public class Client { 4 5 public static void main(String[] args) { 6 Facade facade = new Facade(); 7 facade.MethodA(); 8 facade.MethodB(); 9 } 10 11 }