美文网首页
设计模式-装饰者模式

设计模式-装饰者模式

作者: IAmWhoAmI | 来源:发表于2016-07-28 09:50 被阅读9次

动态增加额外的功能


interface DPerson{
    void eat();
}

class DMan implements DPerson{

    @Override
    public void eat() {
        System.out.println("man eat");
    }
}

abstract class Decorator implements DPerson{
    protected DPerson person;

    public void setPerson(DPerson person) {
        this.person = person;
    }

    @Override
    public void eat() {
        person.eat();
    }
}

class ManDecotatorA extends Decorator{
    @Override
    public void eat() {
        super.eat();
        reEat();
        System.out.println("ManDecotatorA");
    }
    public void reEat(){
        System.out.println("eat one more");
    }
}

class ManDecotatorB extends Decorator{
    @Override
    public void eat() {
        super.eat();
        System.out.println("---------");
        System.out.println("ManDecotatorB");
    }
    public void reEat(){
        System.out.println("eat one more");
    }
}

public class DecoratorTest {
    public static void main(String[] args){
        DMan man =new DMan();
        ManDecotatorA a =new ManDecotatorA();
        ManDecotatorB b =new ManDecotatorB();

        a.setPerson(man);
        b.setPerson(a);
        b.eat();

    }
}

相关文章

  • 设计模式

    设计模式 单例模式、装饰者模式、

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • java IO 的知识总结

    装饰者模式 因为java的IO是基于装饰者模式设计的,所以要了解掌握IO 必须要先清楚什么事装饰者模式(装饰者模式...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计

    JavaScript 设计模式核⼼原理与应⽤实践 之 结构型设计模式 装饰器模式,又名装饰者模式。它的定义是“在不...

  • 8种设计模式:

    主要介绍 单例设计模式,代理设计模式,观察者设计模式,模板模式(Template), 适配器模式,装饰模式(Dec...

  • 装饰者模式

    JavaScript 设计模式 张容铭第十二章 房子装修--装饰者模式 (102页) 装饰者模式(Decorato...

  • Summary of February 2017

    READING Head First 设计模式:完成50%。内容:观察者模式、装饰者模式、工厂模式、单件模式、命令...

  • 装饰对象:装饰者模式

    装饰对象:装饰者模式   这是《Head First设计模式(中文版)》第三章的读书笔记。   装饰者模式,可以称...

  • 设计模式之装饰器模式

    也称装饰者模式、装饰器模式、Wrapper、Decorator。 装饰模式是一种结构型设计模式,允许你通过将对象放...

网友评论

      本文标题:设计模式-装饰者模式

      本文链接:https://www.haomeiwen.com/subject/fbaojttx.html