【设计模式-05】Facade门面Mediator调停者 | Decorator装饰器 | Chain Of Responsibility责任链

Facade门面Mediator调停者

1、Facade门面图解

2、Mediator调停者

一般是系统内部相互交错,比如**消息中间件(MQ)**就是这种设计模式,对各个功能或系统之间进行解耦。


Decorator装饰器

1、问题

2、解决方案


Chain Of Responsibility责任链

一、例子场景

业务场景:论坛或者博客发表帖子或者评论,对敏感词过滤处理。使用责任链的模式进行过滤处理。

二、没有使用设计模式之前

/**
* @description: 责任链模式
* @author: flygo
* @time: 2022/7/18 09:51
*/
public class CorMain {
    
    public static void main(String[] args) {
        Msg msg = new Msg();
        msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");
        
        // 处理msg
        String r = msg.getMsg();
        r = r.replace('<', '[');
        r = r.replace('>', ']');
        msg.setMsg(r);
        
        r = r.replaceAll("996", "955");
        msg.setMsg(r);
        
        System.out.println(msg);
    }
}

class Msg {
    String name;
    String msg;
    
    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    @Override
    public String toString() {
        return "Msg{" + "msg='" + msg + '\'' + '}';
    }
}

二、使用Filter封装处理

把变化的进行封装处理

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class CorMain {

  public static void main(String[] args) {
    Msg msg = new Msg();
    msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");

    // 处理msg
    new HTMLFilter().doFilter(msg);
    new SensitiveFilter().doFilter(msg);

    System.out.println(msg);
  }
}

class Msg {
  String name;
  String msg;

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "Msg{" + "msg='" + msg + '\'' + '}';
  }
}

interface Filter {
  void doFilter(Msg m);
}

class HTMLFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    String r = m.getMsg();
    r = r.replace('<', '[');
    r = r.replace('>', ']');
    m.setMsg(r);
  }
}

class SensitiveFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();

    r = r.replaceAll("996", "955");
    m.setMsg(r);
  }
}

三、使用Filter封装的串在一起

核心代码实现

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class CorMain {

  public static void main(String[] args) {
    Msg msg = new Msg();
    msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");

    List<Filter> filterList = new ArrayList<>();
    filterList.add(new HTMLFilter());
    filterList.add(new SensitiveFilter());

    for (Filter f : filterList) {
      f.doFilter(msg);
    }

    System.out.println(msg);
  }
}

class Msg {
  String name;
  String msg;

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "Msg{" + "msg='" + msg + '\'' + '}';
  }
}

interface Filter {
  void doFilter(Msg m);
}

class HTMLFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    String r = m.getMsg();
    r = r.replace('<', '[');
    r = r.replace('>', ']');
    m.setMsg(r);
  }
}

class SensitiveFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();

    r = r.replaceAll("996", "955");
    m.setMsg(r);
  }
}

四、使用Filter链条串一起

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class CorMain {

  public static void main(String[] args) {
    Msg msg = new Msg();
    msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");

    FilterChain filterChain = new FilterChain();
    filterChain.add(new HTMLFilter());
    filterChain.add(new SensitiveFilter());

    filterChain.doFilter(msg);

    System.out.println(msg);
  }
}

class Msg {
  String name;
  String msg;

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "Msg{" + "msg='" + msg + '\'' + '}';
  }
}

interface Filter {
  void doFilter(Msg m);
}

class HTMLFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    String r = m.getMsg();
    r = r.replace('<', '[');
    r = r.replace('>', ']');
    m.setMsg(r);
  }
}

class SensitiveFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();

    r = r.replaceAll("996", "955");
    m.setMsg(r);
  }
}

class FilterChain {
  List<Filter> filterList = new ArrayList<>();

  public void add(Filter filter) {
    filterList.add(filter);
  }

  public void doFilter(Msg m) {
    for (Filter f : filterList) {
      f.doFilter(m);
    }
  }
}

五、两个链条串一起

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class CorMain {

  public static void main(String[] args) {
    Msg msg = new Msg();
    msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");

    FilterChain filterChain1 = new FilterChain();
    filterChain1.add(new HTMLFilter()).add(new SensitiveFilter());

    FilterChain filterChain2 = new FilterChain();
    filterChain2.add(new FaceFilter()).add(new URLFilter());

    filterChain1.add(filterChain2);

    filterChain1.doFilter(msg);

    System.out.println(msg);
  }
}

class Msg {
  String name;
  String msg;

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "Msg{" + "msg='" + msg + '\'' + '}';
  }
}

interface Filter {
  void doFilter(Msg m);
}

class HTMLFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    String r = m.getMsg();
    r = r.replace('<', '[');
    r = r.replace('>', ']');
    m.setMsg(r);
  }
}

class SensitiveFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();

    r = r.replaceAll("996", "955");
    m.setMsg(r);
  }
}

class FaceFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();
    r = r.replace(":)", "^v^");
    m.setMsg(r);
  }
}

class URLFilter implements Filter {

  @Override
  public void doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();
    r = r.replace("baidu.com", "https://www.baidu.com");
    m.setMsg(r);
  }
}

class FilterChain implements Filter {
  List<Filter> filterList = new ArrayList<>();

  public FilterChain add(Filter filter) {
    filterList.add(filter);
    return this;
  }

  @Override
  public void doFilter(Msg m) {
    for (Filter f : filterList) {
      f.doFilter(m);
    }
  }
}

六、由FilterChain中的某一个Filter决定链条是否继续

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class CorMain {

  public static void main(String[] args) {
    Msg msg = new Msg();
    msg.setMsg("大家好:), <script> 欢迎访问 baidu.com,大家都是996");

    FilterChain filterChain1 = new FilterChain();
    filterChain1.add(new HTMLFilter()).add(new SensitiveFilter());

    FilterChain filterChain2 = new FilterChain();
    filterChain2.add(new FaceFilter()).add(new URLFilter());

    filterChain1.add(filterChain2);

    filterChain1.doFilter(msg);

    System.out.println(msg);
  }
}

class Msg {
  String name;
  String msg;

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  @Override
  public String toString() {
    return "Msg{" + "msg='" + msg + '\'' + '}';
  }
}

interface Filter {
  boolean doFilter(Msg m);
}

class HTMLFilter implements Filter {

  @Override
  public boolean doFilter(Msg m) {
    String r = m.getMsg();
    r = r.replace('<', '[');
    r = r.replace('>', ']');
    m.setMsg(r);

    return true;
  }
}

class SensitiveFilter implements Filter {

  @Override
  public boolean doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();
    r = r.replaceAll("996", "955");
    m.setMsg(r);

    return false;
  }
}

class FaceFilter implements Filter {

  @Override
  public boolean doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();
    r = r.replace(":)", "^v^");
    m.setMsg(r);
    return true;
  }
}

class URLFilter implements Filter {

  @Override
  public boolean doFilter(Msg m) {
    // 处理msg
    String r = m.getMsg();
    r = r.replace("baidu.com", "https://www.baidu.com");
    m.setMsg(r);
    return true;
  }
}

class FilterChain implements Filter {
  List<Filter> filterList = new ArrayList<>();

  public FilterChain add(Filter filter) {
    filterList.add(filter);
    return this;
  }

  @Override
  public boolean doFilter(Msg m) {
    for (Filter f : filterList) {
      if (!f.doFilter(m)) {
        return false;
      }
    }
    return false;
  }
}

七、模拟Java中的ServletFilter 和FilterChain实现

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 责任链模式
 * @author: flygo
 * @time: 2022/7/18 09:51
 */
public class ServletMain {

  public static void main(String[] args) {
    Request request = new Request();
    request.str = "大家好:), <script> 欢迎访问 baidu.com,大家都是996";

    Response response = new Response();
    response.str = "response";

    FilterChain filterChain = new FilterChain();
    filterChain.add(new HTMLFilter()).add(new SensitiveFilter());

    filterChain.doFilter(request, response);

    System.out.println(request.str);
    System.out.println(response.str);
  }
}

class Request {
  String str;
}

class Response {
  String str;
}

interface Filter {
  boolean doFilter(Request request, Response response, FilterChain chain);
}

class HTMLFilter implements Filter {

  @Override
  public boolean doFilter(Request request, Response response, FilterChain chain) {
    request.str = request.str.replaceAll("<", "[").replaceAll(">", "]") + "----HTMLFilter";
    chain.doFilter(request, response);
    response.str += "----HTMLFilter";
    return true;
  }
}

class SensitiveFilter implements Filter {

  @Override
  public boolean doFilter(Request request, Response response, FilterChain chain) {
    request.str = request.str.replaceAll("996", "955") + "----SensitiveFilter";
    chain.doFilter(request, response);
    response.str += "----SensitiveFilter";
    return true;
  }
}

class FilterChain {
  List<Filter> filterList = new ArrayList<>();
  int index = 0;

  public FilterChain add(Filter filter) {
    filterList.add(filter);
    return this;
  }

  public boolean doFilter(Request request, Response response) {
    if (filterList.size() == index) {
      return false;
    }
    Filter f = filterList.get(index);
    index++;

    return f.doFilter(request, response, this);
  }
}

八、源码地址

GitHub - jxaufang168/Design-Patterns: 设计模式学习设计模式学习. Contribute to jxaufang168/Design-Patterns development by creating an account on GitHub.https://github.com/jxaufang168/Design-Patterns

相关推荐
小白不太白9501 小时前
设计模式之建造者模式
java·设计模式·建造者模式
菜菜-plus3 小时前
java 设计模式 模板方法模式
java·设计模式·模板方法模式
萨达大3 小时前
23种设计模式-模板方法(Template Method)设计模式
java·c++·设计模式·软考·模板方法模式·软件设计师·行为型设计模式
机器视觉知识推荐、就业指导5 小时前
C++设计模式:原型模式(Prototype)
c++·设计模式·原型模式
阳光开朗_大男孩儿5 小时前
组合模式和适配器模式的区别
设计模式·组合模式·适配器模式
MinBadGuy6 小时前
【GeekBand】C++设计模式笔记13_Flyweight_享元模式
c++·设计模式
Clang's Blog7 小时前
23种设计模式详解(以Java为例)
java·开发语言·设计模式
程序员奇奥7 小时前
设计模式——简单工厂模型、工厂模式、抽象工厂模式、单例模式、代理模式、模板模式
单例模式·设计模式·抽象工厂模式
hxj..7 小时前
【设计模式】代理模式
java·设计模式·代理模式·动态代理
十五年专注C++开发8 小时前
C++不完整类型(Incomplete Type)的检测与避免
开发语言·c++·算法·设计模式