Facade门面,Mediator调停者,Decorator装饰器,责任链
Facade 门面
- 对外,专业代办
Mediator 调停者
- 部门内部打交道,从内部抽出一个部门来,这个部门就是调停者
对内和对外可以是一个人,心中无剑,手中有剑
Decorator 装饰器
用聚合代替继承
责任链 ChainOfResponsibility
- 封装变化
public class Main{ public static void main(String[] args){ Msg msg = new Msg(); msg.setMsg("welcome"); FilterChain fc1 = new FilterChain(); fc1.add(new HTMLFilter()).add(new SensitiveFilter()); FilterChain fc2 = new FilterChain(); fc2.add(new FaceFilter()).add(new UrlFilter()); fc.doFilter(msg); fc2.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; } public String toString(){ return "Msg{" + "msg='" + msg + '\'' + '}'; } interface Filetr{ void doFilter(Msg m); } class HTMLFilter implements Filter{ public boolean doFilter(Msg m){ String r = m.getMsg(); r = r.replace('<', '['); r = r.replace('>', ']'); m.setMsg(r); return true; } } class SensitiveFilter implements Filter{ public boolean doFilter(Msg m){ r = r.replaceAll("wel", "in"); m.setMsg(r); return false; } } class FaceFilter implements Filter{ public boolean doFilter(Msg m){ r = r.replaceAll("wel", ":)"); m.setMsg(r); return false; } } class UrlFilter implements Filter{ public boolean doFilter(Msg m){ r = r.replaceAll("wel", "http://"); m.setMsg(r); return false; } } class SensitiveFilter implements Filter{ public boolean doFilter(Msg m){ r = r.replaceAll("wel", "in"); m.setMsg(r); return false; } } class FilterChain{ List<Filter> filters = new ArrayList<>(); public FilterChain add(Filter f){ filters.add(f); return this; } public boolean doFilter(Msg m){ for(Filter f : filters){ if(!f.doFilter(m)){ return false; } return true; } } } class Request{ String str; } class Response{ String str; }
论读书
睁开眼,书在面前 闭上眼,书在心里
睁开眼,书在面前 闭上眼,书在心里