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

来源:互联网 发布:杨氏模量测量实验数据 编辑:程序博客网 时间:2024/05/20 13:20

一、装饰模式(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();    }}



可怜设计模式好有意思



原创粉丝点击