设计模式系列 - 门面模式(Facade)

 门面模式

    所谓的门面模式,是指向外统一外观,使用户的调用更加的方便, 因为客户调用的服务很可能分布在多个接口之中。可以按照模块进行facade,也可以按照功能进行facade。好比卖早餐的小店, 只需要向外提供一个通道即可,通过这个通道可以卖油条豆浆(一个接口)、可以卖包子稀饭(又一个接口)、还可以卖牛奶面包(又一个接口), 但是他们之后很难建立统一的接口来覆盖到所有的接口, 然而客户只需要通过这个通道可以买食品即可。 有时候各接口的方法之间还存在一些业务关系,需要相互操作完成之后才能向外提供服务。在远程调用的时候,使用起来效果更加理想。

  GOF中关于门面模式的描述是: 门面模式为了系统提供一个 统一的高层接口供外部客户使用。也是提供一个更高的层次的接口,使得子系统更容易使用。

   1) 它可以对客户屏蔽子系统组件,因而减少了客户处理的对象的数目并使得子系统使用起来更加方便。 2) 它实现了子系统与客户之间的松耦合关系,而子系统内部的功能组件往往是紧耦合的。松耦合关系使得子系统的组件变化不会影响到它的客户。

facade

 中餐接口:ChineseFood.java

  
  
  
  
  1. /** 
  2.  * 中餐关心的是色香味、价格 
  3.  * @author keju.wangkj 
  4.  * 
  5.  */ 
  6. public interface ChineseFood { 
  7.     String getSmell(); 
  8.     double price(); 

快餐接口: FastFood.java

  
  
  
  
  1. /** 
  2.  * 快餐关心的是速度、是否卫生 
  3.  * @author keju.wangkj 
  4.  * 
  5.  */ 
  6. public interface FastFood { 
  7.     long timeCost(); 
  8.     boolean isHealthy(); 

 

门面模式统一接口:ChannelFacade.java

  
  
  
  
  1. /** 
  2.  * 提供食品的通道Facade模式接口 
  3.  * @author keju.wangkj 
  4.  * 
  5.  */ 
  6. public interface ChannelFacade { 
  7.     String getSmell(); 
  8.  
  9.     double price(); 
  10.  
  11.     long timeCost(); 
  12.  
  13.     boolean isHealthy(); 

 门面模式具体的实现类:ChannelImpl.java

  
  
  
  
  1. public class ChannelImpl implements ChannelFacade { 
  2.     private ChineseFood chineseFood; 
  3.     private FastFood fastFood; 
  4.     public String getSmell() { 
  5.         return chineseFood.getSmell(); 
  6.     } 
  7.  
  8.     public boolean isHealthy() { 
  9.         return fastFood.isHealthy(); 
  10.     } 
  11.  
  12.     public double price() { 
  13.         return chineseFood.price(); 
  14.     } 
  15.  
  16.     public long timeCost() { 
  17.         return fastFood.timeCost(); 
  18.     } 
  19.  

中餐的 具体的实现:ChineseFoodImpl.java

  
  
  
  
  1. public class ChineseFoodImpl implements ChineseFood{ 
  2.  
  3.     public String getSmell() { 
  4.         return "smell good, delicious"
  5.     } 
  6.  
  7.     public double price() { 
  8.         return 12.0
  9.     } 
  10.  

 

快餐的具体实现:FastFoodImpl.java

  
  
  
  
  1. public class FastFoodImpl implements FastFood{ 
  2.  
  3.     public boolean isHealthy() { 
  4.         return true
  5.     } 
  6.  
  7.     public long timeCost() { 
  8.         //5分钟搞定,快餐就是快,呵呵 
  9.         return 5
  10.     } 
  11.  

测试类:ChannelFacadeTest.java 

  
  
  
  
  1. import org.junit.Before; 
  2. import org.junit.Test; 
  3.  
  4. public class ChannelFacadeTest { 
  5.     ChannelFacade channelFacade; 
  6.     ChannelImpl channelImpl = new ChannelImpl(); 
  7.     @Before 
  8.     public void initial(){ 
  9.         channelImpl.setChineseFood(new ChineseFoodImpl()); 
  10.         channelImpl.setFastFood(new FastFoodImpl()); 
  11.         channelFacade = channelImpl; 
  12.     } 
  13.  
  14.     @Test 
  15.     public void testGetSmell() { 
  16.         System.out.println(channelFacade.getSmell()); 
  17.     } 
  18.  
  19.     @Test 
  20.     public void testPrice() { 
  21.         System.out.println(channelFacade.price()); 
  22.     } 
  23.  
  24.     @Test 
  25.     public void testTimeCost() { 
  26.         System.out.println(channelFacade.timeCost()); 
  27.     } 
  28.  
  29.     @Test 
  30.     public void testIsHealthy() { 
  31.         System.out.println(channelFacade.isHealthy()); 
  32.     } 
  33.  

 运行结果:

 smell good, delicious

12.0

5

true

 

 

 

你可能感兴趣的:(职场,休闲,门面模式)