门面模式 实现了子模块 与客户端 之间的松耦合 关系,从而屏蔽了 子模块内部实现的细节,只是将客户端需要的 接口提供给客户,使得子模块的组件如果发生变化不会影响客户端的使用,“松耦合,高内聚” 的体现。
一个薪水结算的例子
1 public class Salary { 2 public double getSalary(String empno){ 3 return 200; 4 } 5 6 }
1 public class Holiday { 2 public double getHoliday(String empno){ 3 return 2; 4 } 5 }
1 public class Tax { 2 public double getTax(String empno){ 3 return 0; 4 } 5 }
1 public interface Computer { 2 public double doSalary(String empno); 3 }
1 public class ComputerSalary implements Computer { 2 3 @Override 4 public double doSalary(String empno) { 5 Salary salary = new Salary(); 6 Holiday holiday = new Holiday(); 7 Tax tax = new Tax(); 8 Double money = salary.getSalary(empno) * (30 - holiday.getHoliday(empno))-tax.getTax(empno); 9 return money; 10 } 11 12 }
1 import java.io.IOException; 2 import java.io.InputStream; 3 import java.util.Properties; 4 5 public class PropertiesUtil { 6 public static Object getInstance() { 7 Object obj = null; 8 try { 9 Properties p = new Properties(); 10 InputStream in = null; 11 in = Client.class.getResourceAsStream("class.properties"); 12 try { 13 p.load(in); 14 } catch (IOException e) { 15 16 e.printStackTrace(); 17 } finally { 18 try { 19 in.close(); 20 } catch (IOException e) { 21 22 e.printStackTrace(); 23 } 24 } 25 26 obj = (Object) Class.forName(p.getProperty("class")).newInstance(); 27 } catch (InstantiationException e) { 28 29 e.printStackTrace(); 30 } catch (IllegalAccessException e) { 31 32 e.printStackTrace(); 33 } catch (ClassNotFoundException e) { 34 35 e.printStackTrace(); 36 } 37 return obj; 38 } 39 40 41 42 }
1 public class Client { 2 public static void main(String[] args) { 3 Computer computer = (Computer) PropertiesUtil.getInstance(); 4 System.out.println(computer.doSalary("1994"));// 调用实现类方法 5 } 6 7 }
包下的 配置文件 class.properties
class = com.facade.ComputerSalary
时间: 2024-11-08 18:21:24