JAVA设计模式之外观模式

本文深入探讨了外观模式的定义、优点、参与者、缺点和实际应用案例。外观模式旨在为复杂的子系统提供一个简洁的统一接口,简化客户端与其交互,降低耦合度。文章通过代码示例展示了如何在Java中实现外观模式,并提到了Spring JDBC、MyBatis Configuration和Tomcat Request/Response Facade等实际应用场景。
摘要由CSDN通过智能技术生成

1. 定义

外观模式是为了给客户端的一个一组接口提供一个统一的接口,是子系统更容易使用

2. 优点

更容易使用,客户端不需要和子系统在做更多的交互,也不需要了解子系统更多的实现细节
将客户端与子系统解耦,让子系统内部模块更容易扩展和使用
可以更好的隐藏子系统的内部细节

3. 参与者

1 facade 实现子系统的门面
2 subclass 子系统

4. 缺点

增加子系统,扩展子系统容易引入风险

不符合开闭原则

5. 类型

结构

6. coding

package com.maidou.learning.design.structure.facade;

public class FacadeA {
   private IService serviceA = new ServiceAImpl();
   private IService serviceB = new ServiceBImpl();
 
   
   public FacadeA() {
   }

   public void getService() {
      serviceA.method();;
      serviceB.method();
   }
}

package com.maidou.learning.design.structure.facade;

public interface IService {
    void method();
}

package com.maidou.learning.design.structure.facade;

public class ServiceAImpl implements IService {
   @Override
   public void method() {
      System.out.println("a 实现");
   }
}


package com.maidou.learning.design.structure.facade;

public class ServiceBImpl implements IService {
   @Override
   public void method() {
      System.out.println("b 实现");
   }
}

package com.maidou.learning.design.structure.facade;

public class FacadeA {
   private IService serviceA = new ServiceAImpl();
   private IService serviceB = new ServiceBImpl();
 
   
   public FacadeA() {
   }

   public void getService() {
      serviceA.method();;
      serviceB.method();
   }
}

7. 源码应用

  1. spring jdbcutils
  2. mybatis configuation
  3. tomcat requestFacade responseFacade
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值