Note

春蚕到死丝方尽,人至期颐亦不休,一息尚存须努力,留作青年为范畴。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

介绍

意图:动态地给一个对象添加一些额外的职责。就增加功能来说, D e c o r a t o r模式相比生成子类更为灵活。

使用场景:处理规则可以任意组装的时候

结构图:

 

 代码:

//过滤功能接口

package note.com.decorator;

public interface Deal {
    public void deal();
    
    public String read();
}

//过滤功能接口实现

package note.com.decorator;

public class HtmlDeal implements Deal{
private String string = null;
    
    public HtmlDeal(String str) {
        string = str;
    }

    public void deal() {
        System.out.println("HTML处理:"+ read());
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public String  read() {
        return string;
    }

}

//装饰过滤接口基类

package note.com.decorator;

/**
 * 符合装饰规则的
 * @author lxz
 *
 */
public abstract class Decorator implements Deal{
    private Deal deal = null;
    public  Decorator(Deal deal) {
        this.deal = deal;
    }
    
    public void deal() {
        deal.deal();
    }
    
    public String read() {
        return deal.read();
    }
}

//装饰器子类1

package note.com.decorator;

public class ChineseDeal extends Decorator{
    
    public ChineseDeal(Deal deal) {
        super(deal);
    }

    public void deal() {
        System.out.println("Chinese处理:"+read());
        super.deal();
    }

    public String read() {
        return super.read();
    }

}

//装饰器子类2

package note.com.decorator;

public class EnglishDeal extends Decorator{

    public EnglishDeal(Deal deal) {
        super(deal);
    }

    public void deal() {
        System.out.println("English处理:"+read());
        super.deal();
    }

    public String read() {
        return super.read();
    }

}

//测试类

package note.com.decorator;

import java.lang.reflect.InvocationTargetException;

public class DecaratorTest {
    
    public static void main(String[] args) throws ClassNotFoundException,
            NoSuchMethodException, SecurityException, InstantiationException,
            IllegalAccessException, IllegalArgumentException,
            InvocationTargetException {
        Deal html = new HtmlDeal("请处理我这一串字符串");
        Deal chinese = new ChineseDeal(html);
        Deal english = new EnglishDeal(chinese);
        english.deal();

    }
}

 //结果

English处理:请处理我这一串字符串
Chinese处理:请处理我这一串字符串
HTML处理:请处理我这一串字符串

posted on 2016-03-02 16:28  'Note'  阅读(202)  评论(0编辑  收藏  举报