zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Decorator模式及其他相似的模式

模式 及其 相似 Decorator
2023-09-11 14:20:55 时间
    private MessageBoardHandler handler;        public MessageBoardDecorator(MessageBoardHandler handler) {            super();            this.handler = handler;          }        public String filter(String msg) {            return handler.filter(msg);          }   package com.whatsmars.tomcat.design.decorator;   * @author javahongxi 具体装饰角色,增加过滤掉HTML标签的功能  public class HtmlFilter extends MessageBoardDecorator {       public HtmlFilter(MessageBoardHandler handler) {           super(handler);       }       public String filter(String content) {           String temp = super.filter(content);           temp += "^^过滤掉HTML标签!^^";           return temp;          }   package com.whatsmars.tomcat.design.decorator;   * @author javahongxi 具体装饰角色,增加过滤掉政治敏感字眼的功能  public class SensitiveFilter extends MessageBoardDecorator {       public SensitiveFilter(MessageBoardHandler handler) {           super(handler);       }       public String filter(String content) {           String temp = super.filter(content);           temp += "^^过滤掉政治敏感的字眼!^^";           return temp;       }   package com.whatsmars.tomcat.design.decorator;   * @author javahongxi 客户端测试  public class Test {       public static void main(String[] args) {           MessageBoardHandler mb = new MessageBoard();           String content = mb.filter("一定要学好装饰模式!");           System.out.println(content);                        mb = new HtmlFilter(new SensitiveFilter(new MessageBoard()));           content = mb.filter("一定要学好装饰模式!");           System.out.println(content);       }   console:   留言板上的内容:一定要学好装饰模式!   留言板上的内容:一定要学好装饰模式!^^过滤掉政治敏感的字眼!^^^^过滤掉HTML标签!^^  
public class Adapter2 extends Adaptee implements Target {       // 对于我们不必要实现的方法可在Adaptee中作空实现       public void request() {           super.specificRequest();       }  

  Facade模式

  -- 见一个简单的Servlet容器

 

  个人认为上述三种模式可以统称Wrapper模式,熟练之后我们不必在意它们究竟属于何种设计模式,当然类的命名最好按具体的模式来。



原文链接:[http://wely.iteye.com/blog/2290854]


在空对象模式(Null Object Pattern)中,一个空对象取代 NULL 对象实例的检查。Null 对象不是检查空值,而是反应一个不做任何动作的关系。