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);
}
}