大话设计模式读后感之装饰模式

本文详细介绍了装饰模式的概念及其在Java中的应用实例。通过具体的代码示例,展示了如何利用装饰模式为对象动态添加职责,并讨论了其相对于子类扩展的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、装饰模式(Decorator)

动态地给一个对象添加一个额外的职责,就增加功能来说,装饰模式比子类更为灵活。


其实我们对装饰模式并不陌生,在JDK中使用装饰模式设计的类的有以下。

•java.io.BufferedInputStream(InputStream)
•java.io.DataInputStream(InputStream)
•java.io.BufferedOutputStream(OutputStream)
•java.util.zip.ZipOutputStream(OutputStream)
•java.util.Collections#checked[List|Map|Set|SortedSet|SortedMap]()

简单装饰模式

Component类

/**
 * Created with Intellij IDEA.
 * User : ybh
 * Created on 2017/8/4.
 * Description :
 * Component 是定义一个对象的接口,可以给这些对象动态添加职责
 */
public abstract class Component {

    public  abstract void Operation();
}

ConcreteComponent 类
/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 * ConcreteComponent 定义了一个具体对象,也可以给这个对象添加一些职责
 */
public class ConcreteComponent extends  Component {

    @Override
    public void Operation() {
        System.err.println("具体对象操作");
    }
}
Decorator 类
/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 * Decorator    装饰抽象类
 *              继承了Component,作为子类扩展Component类功能,
 *              Component并不需要知道Decorator的存在
 */
public abstract class Decorator extends  Component {

    protected  Component component;


    /**
     * 设置Component
     * @param component
     */
    public void setComponent(Component component) {
        this.component = component;
    }

    /**
     * 重写Operation 实际执行的是compoent
     */
    @Override
    public void Operation() {
        if (component!=null){
            component.Operation();
        }
    }
}


装饰类A
/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 */
public class ConcreteDecoratorA  extends Decorator{
    //独有属性区别于装饰B
    private String addedState;

    @Override
    public void Operation() {
        //运行Component的方法
        super.Operation();
        addedState="New State";
        System.err.println("具体装饰对象A的操作");
    }

}


装饰类B

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 */
public class ConcreteDecoratorB extends Decorator {

    /*
    独有方法
     */
    public  void addedBehavior(){

    }

    @Override
    public void Operation() {
        //运行Component的方法
        super.Operation();
        addedBehavior();
        System.err.println("具体装饰对象B的操作");
    }
}
Main

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 */
public class TestMain {

    public static void main(String[] args) {

        //具体对象
        ConcreteComponent c=new ConcreteComponent();
        ConcreteDecoratorA cA=new ConcreteDecoratorA();
        ConcreteDecoratorB cB=new ConcreteDecoratorB();
        //对象包装
        cA.setComponent(c);
        cB.setComponent(cA);
        cB.Operation();

        /**
         * 装饰模式利用setComponent进行对象包装,每个装饰对象的实现和使用分离,
         * 每个装饰对象只要关系自己的功能,不需要关心如何被使用
         */


    }
}

例子

ConcreteComponent类

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 *  该类为我们需要装饰的具体类
 */
public class Person {
    //人名
   private String name;

    public Person(){

    }


   public Person(String name){
       this.name=name;
   }

   public void show(){
       System.err.println(name+"时装有");
   }
}


Decorate类

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 * 装饰类
 */
public class Finery extends Person {

    private Person person;


    /*
    装饰对象
     */
    public void Decorate(Person person){
        this.person=person;
    }

    @Override
    public void show() {
        if (person!=null){
            person.show();
        }
    }
}


装饰A

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 *  装饰方法之一
 */
public class TShirts extends  Finery {


    @Override
    public void show() {
        super.show();
        System.err.println("T恤");

    }
}

装饰B

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 *  装饰方法之一
 */
public class Pants extends  Finery {


    @Override
    public void show() {
        super.show();
        System.err.println("短裤");


    }
}


Main类

/**
 * Created with Intellij IDEA.
 * User : Yebinghuan
 * Created on 2017/8/4.
 * Description :
 */
public class Main {

    public static void main(String[] args) {
        Person Person=new Person("七月");
        Finery finery=new Finery();
        TShirts tShirts=new TShirts();
        Pants t=new Pants();

        finery.Decorate(Person);
        tShirts.Decorate(finery);
        t.Decorate(tShirts);
        t.show();



    }
}



可怜设计模式好有意思



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值