设计模式-装饰器模式(Decorator)

1.意图

动态地给一个对象添加一些额外的功能.

2.适用性

  • 动态、透明的方式给单个对象添加职责。

  • 如果不适合适用子类来进行扩展的时候,可以考虑适用装饰模式。

  • 避免子类数目爆炸性增长。

3.结构

装饰模式结构

4.参与者

  • Component: 定义一个对象接口,可以给这些对象动态地添加职责.
  • ConcreteComponent: 定义一个对象,可以给这个对象添加职责.
  • Decorator: 持有一个指向Component对象的引用,并定义一个与Component的接口一致的接口.
  • ConcreteComponent: 向组件添加职责.

5.效果

1)优点:
  • 比静态继承更灵活.
  • 避免在层次结果高层的类有太多的特征.
2)缺点:
  • 产生许多小对象,增加对象复杂度,不易排错.

6.模式应用

  • Java中的I/O流
  • 面向切面编程AOP(思想相似)

7.实例

1)场景

一个软件公司由员工Employee组成,员工又分为开发人员Dev、领导Leader、经理Manager、QA部门领导QALeader。Dev职责是开发代码;QALeader职责不但能开发代码,而且能设计测试案例,写测试报告;Manager职责还能开发框架,发布版本。

2)UML图

UML图

3)代码

Employee


public abstract class Employee {
    public abstract void doSomething();
}

Dev


public class Dev extends Employee {
    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        writeCode();
    }
    private void writeCode() {
        // TODO Auto-generated method stub
        System.out.println("I'm a programer,complete the code!");
        System.out.println("---------------------------------------");
    }
}

Leader


public abstract class Leader extends Employee {
    private Employee person;
    public Leader(Employee person) {
        super();
        this.person = person;
    }
    @Override
    public void doSomething() {
        person.doSomething();
    }
}

Manager


public class Manager extends Leader {
    public Manager(Employee person) {
        super(person);
        // TODO Auto-generated constructor stub
    }
    public  void doSomething(){
        begin();
        super.doSomething();
        end();
    }
    private void begin() {
        // TODO Auto-generated method stub
        System.out.println("Design the framework!");
        System.out.println("---------------------------------------");
    }
    private void end() {
        // TODO Auto-generated method stub
        System.out.println("Release the verion!");
        System.out.println("---------------------------------------");
    }
}

QALeader


public class QALeader extends Leader {
    public QALeader(Employee person){
        super(person);
    }

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub
        begin();
        super.doSomething();
        end();
    }
    private void end() {
        // TODO Auto-generated method stub
        System.out.println("Write the test reporter");
        System.out.println("---------------------------------------");
    }
    private void begin() {
        // TODO Auto-generated method stub
        System.out.println("Write the test cases!");
        System.out.println("---------------------------------------");
    }
}

输出结果:

========leader1 doSomething==========

Write the test cases!
—————————————
I’m a programer,complete the code!
—————————————
Write the test reporter
—————————————
========leader2 doSomething==========
Design the framework!
—————————————
Write the test cases!
—————————————
I’m a programer,complete the code!
—————————————
Write the test reporter
—————————————
Release the verion!
—————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值