欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Decorator模式及其他相似的模式

程序员文章站 2022-05-05 12:00:45
...

 Decorator模式

package com.whatsmars.tomcat.design.decorator;

/**
* @author javahongxi 用户留言板处理的接口
*/
public interface MessageBoardHandler {
    /**
	* @author javahongxi 用户可以利用函数留言
	*/
	public String filter(String msg);
}

package com.whatsmars.tomcat.design.decorator;

/**
* @author javahongxi 用户留言板的具体实现
*/
public class MessageBoard implements MessageBoardHandler {

	public String filter(String msg) {
		return "留言板上的内容:" + msg;
	}
}

package com.whatsmars.tomcat.design.decorator;

/**
* @author javahongxi 装饰角色
*/
public class MessageBoardDecorator implements MessageBoardHandler {

	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标签!^^

 

 Adapter模式

package com.whatsmars.tomcat.design.adapter;

/**
 * Created by shenhongxi on 16/4/14.
 */
public class Adaptee {

    public void specificRequest() {
        System.out.println("specific request");
    }
}

package com.whatsmars.tomcat.design.adapter;

/**
 * Created by shenhongxi on 16/4/14.
 */
public interface Target {

    public void request();
}

package com.whatsmars.tomcat.design.adapter;

/**
 * Created by shenhongxi on 16/4/14.
 */
public class Adapter implements Target {

    Adaptee adaptee;

    public void request() {
        adaptee.specificRequest();
    }
}

package com.whatsmars.tomcat.design.adapter;

/**
 * Created by shenhongxi on 16/4/14.
 */
public class Adapter2 extends Adaptee implements Target {
    // 对于我们不必要实现的方法可在Adaptee中作空实现
    public void request() {
        super.specificRequest();
    }
}

  Facade模式

  -- 见一个简单的Servlet容器

 

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