设计模式

设计模式--外观模式(Facade)

2019-03-21  本文已影响0人  二妹是只猫

模式介绍

定义与类型

使用场景

优点

缺点

UML类图:

Facade-Pattern.png

简单示例

public class MobilePhone{
    private Phone mPhone = new phoneImpl();
    private Camera mCamera = new CameraImpl();

    public void deil(){
        mPhone.dail();
    }

    public void close(){
        mPhone.hangup();
    }

    public void takePicture(){
        mCamera.takePicture();
    }
}
public interface Phone{
    //打电话
    void dail();
    //挂电话
    void hangup();
}
public interface Camera{
    //拍照片
    void takePicture();
}
public class phoneImpl implements Phone{

    @Override
    public void dail() {
        System.out.println("打电话");
    }

    @Override
    public void hangup() {
        System.out.println("挂电话");
    }
}
public class cameraImpl implements Camera{

    @Override
    public void takePicture() {
        System.out.println("拍照片");
    }
}
public static void main(String[] args) {
      MobilePhone s10 = new MobilePhone();
      s10.deil();
      s10.hashCode();
  }

外观模式在源码中的应用

public class Configuration {
  '省略代码'
  protected final InterceptorChain interceptorChain = new InterceptorChain();
  '省略代码'
  public ParameterHandler newParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
    ParameterHandler parameterHandler = mappedStatement.getLang().createParameterHandler(mappedStatement, parameterObject, boundSql);
    parameterHandler = (ParameterHandler) interceptorChain.pluginAll(parameterHandler);
    return parameterHandler;
  }

Configuration是外观类,InterceptorChain是子系统通过外观类Configuration的newParameterHandler方法调用子系统InterceptorChain获取具体产品。

上一篇 下一篇

猜你喜欢

热点阅读