- 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模式,熟练之后我们不必在意它们究竟属于何种设计模式,当然类的命名最好按具体的模式来。
原文链接:[http://wely.iteye.com/blog/2290854]