一天一种设计模式之七-----装饰模式

一.装饰模式定义

  1. 装饰模式属于结构型模式  
    java

  2.  装饰模式动态的给一个对象添加一些额外的职责。就增长功能来讲,decorator模式相比生成子类更加灵活
    ide

  3. 适用性测试

    1. 在不影响其余对象的状况下,以动态、透明的方式给单个对象添加职责this

    2. 处理那些能够撤销的职责spa

    3. 当不能采用生成子类的方法进行填充时。指针

  4. 结构:code

    1. Component定义一个对象接口,能够给这些对象动态的添加职责。component

    2. ConcreteComponent定义一个对象,能够给这个对象添加一些职责。对象

    3. Decorator维持一个纸箱Component对象的指针,并定义一个与Component接口一致的接口。继承

    4. ConcreteDecorator向组件添加职责。

  5. 好处

    1. 把类的核心职责和装饰功能区分开了。能够去除相关类中的重复的装饰逻辑。装饰类之间要彼此独立。

    2. 比继承更多的灵活性。

    3. 在运行时选择不一样的装饰器,扩展对象的功能,增长了灵活性。

    4. 经过装饰类的排列组合,能够创造出不一样行为组合,获得功能更强大的对象。

  6. 缺点

    1. 顺序很重要,不留神可能会出现递归死循环的状况。

    2. 会产生不少小对象,增长了系统复杂性。

    3. 出错时,逐级排查可能会很麻烦(因此不一样装饰器必定要负责不一样的行为,这样便于区分)

  7. 符合开闭原则。迪米特法则。

  8.  我的 感受装饰模式就是把声明好的类当作另外一个类的元素,递归的调用全部父元素到子元素的同一个方法。

  9. BufferedReader等io流用的就是装饰模式,有兴趣的小伙伴能够看看源码研究下。

   

二.测试代码

  1.     测试代码1以下,

public class ZhuangshiMoshiTest2 {
    public static void main(String[] args) {
        Man man=new Man();
        ManDecoratorA decoratorA=new ManDecoratorA();
        ManDecoratorB decoratorB=new ManDecoratorB();
        decoratorA.setPerson(man);
        decoratorB.setPerson(decoratorA);
        decoratorB.eat();
    }
}
interface Human{
    void eat();
}
class Man implements Human{
    @Override
    public void eat() {
        System.out.println("男人在吃");
    }
}
abstract class Decorator implements Human{
    protected Human person;
    public void setPerson(Human person) {
        this.person = person;
    }
    @Override
    public void eat() {
        person.eat();
    }
}
class ManDecoratorA extends Decorator{
    @Override
    public void eat() {
        super.eat();
        reEat();
    }

    private void reEat() {
        System.out.println("再吃顿");
    }
}
class ManDecoratorB extends Decorator{
    @Override
    public void eat() {
        super.eat();
        System.out.println("=====");
        System.out.println("B");
    }
}

    2.测试代码2以下

public class ZhuangshimoshiTest {
    public static void main(String[] args) {
        ConcreateComponent component = new ConcreateComponent() {
        };
        ConcreateComponent component2 = new ConcreateDecorationA();
        ConcreateComponent component3 = new ConcreateDecorationB();
        component2.setComponent(component);
        component3.setComponent(component2);
        component3.operation();
    }
}

abstract class Component {
    public abstract void operation();
}

abstract class ConcreateComponent extends Component {
    protected Component component;

    public void setComponent(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        if (component != null) {
            component.operation();
        }
    }
}

class ConcreateDecorationA extends ConcreateComponent {
    private String addedState;// 本类独有的成员,能够区别去其余类

    @Override
    public void operation() {
        super.operation();
        addedState = "New state";
        System.out.println("装饰类A进行装饰");
    }
}

class ConcreateDecorationB extends ConcreateComponent {
    @Override
    public void operation() {
        super.operation();
        addMethod();
        System.out.println("装饰类B进行装饰");
    }

    private void addMethod() {// 类B独有的方式,能够区别其余类

    }
}

3.组合模式和装饰模式同属结构型模式,组合模式是一种树形结构,而装饰者模式是一种链表结构。感受有一点类似。下节会介绍组合模式以及组合模式与装饰模式的区别。

4.命令模式、职责链模式虽然属于行为型模式,但他们和装饰模式也有一点类似。后面也会一一介绍。敬请期待。

5.最近动力有点不足,没有作到一天一篇,自责下。另外在公司一直在作些改改模版增删改查的东西,事情不少尚未含金量,因此很烦。不忘初心吧。共勉。

相关文章
相关标签/搜索