博客
关于我
设计模式-装饰模式Decorator JAVA示例
阅读量:676 次
发布时间:2019-03-16

本文共 2609 字,大约阅读时间需要 8 分钟。

装饰模式(Decorator Pattern)是一种动态添加对象职责的设计模式,常用于在不改变原有点的情况下,向其添加额外功能。相比于生成子类,这种方式更加灵活,因为它只需包装原对象即可扩展功能。

装饰模式结构

装饰模式通过引入一个装饰器(Decorator)抽象类,统一定义装饰操作。具体实现分为两个层次:

  • 装饰对象(Decorator):提供通用的装饰逻辑。
  • 具体装饰对象(ConcreteDecorator):实现具体功能的装饰。
  • 基本代码示例

    // 抽象类Componentabstract class Component {    public abstract void operation();}// 具体组件Componentclass ConcreteComponent implements Component {    @Override    public void operation() {        // 组件的操作    }}// 抽象装饰器Decoratorabstract class Decorator extends Component {    protected Component mComponent;    public void setComponent(Component component) {        mComponent = component;    }    @Override    public void operation() {        if (mComponent != null) {            mComponent.operation();        }    }}// 具体装饰器ConcreteDecoratorAclass ConcreteDecoratorA extends Decorator {    private String addedState;    @Override    public void operation() {        super.operation(); // 调用被装饰对象        addedState = "New State"; // 添加新状态    }}// 具体装饰器ConcreteDecoratorBclass ConcreteDecoratorB extends Decorator {    @Override    public void operation() {        // 调用被装饰对象        super.operation();    }}// 客户端代码public static void main(String[] args) {    Component mConcreteComponent = new ConcreteComponent();    Decorator mDecoratorA = new ConcreteDecoratorA();    Decorator mDecoratorB = new ConcreteDecoratorB();    mDecoratorA.setComponent(mConcreteComponent);    mDecoratorB.setComponent(mDecoratorA);    mDecoratorB.operation();}

    服装装饰模式实例

    以模拟给人穿衣服的功能为例:

    // 服饰类public class Finery extends Person {    protected Person mPerson = null;    public void decorator(Person person) {        this.mPerson = person;    }    @Override    public void show() {        if (mPerson != null) {            mPerson.show();        }    }}// 具体服饰Itempublic class Tshirts extends Finery {    @Override    public void show() {        super.show();        System.out.println("穿T恤!");    }}// 其他服饰类(如BigTrouser、DuckHat)均类似上述结构// 客户端代码public class DressUpBaby {    public static void main(String[] args) {        Person mPerson = new Person("helloBaby");        Tshirts mTshirts = new Tshirts();        BigTrouser mBigTrouser = new BigTrouser();        DuckHat mDuckHat = new DuckHat();        mTshirts.decorator(mPerson);        mBigTrouser.decorator(mTshirts);        mDuckHat.decorator(mBigTrouser);        mDuckHat.show();        // 通过递归调用,最终显示完整服装效果    }}

    输出结果

    dress up :helloBaby穿T恤。穿大裤子!穿鸭子帽!

    装饰模式优势

  • 灵活性:可以动态添加功能,而无需生成子类。
  • 可组合性:各个装饰功能可按需组合,支持复杂功能堆砌。
  • 可扩展性:新功能只需引入新的装饰类,无需修改现有系统。
  • 可维护性:功能逻辑与装饰对象分离,便于独立开发和维护。
  • 通过以上示例可见,装饰模式是功能扩展的理想选择。它将装饰功能从原有对象中解耦,使组件更加简洁,且支持动态功能升级。在实际应用中,可灵活选择何时何地应用装饰模式,以满足不同的功能需求。

    转载地址:http://hceqz.baihongyu.com/

    你可能感兴趣的文章
    param[:]=param-lr*param.grad/batch_size的理解
    查看>>
    spring mvc excludePathPatterns失效 如何解决spring拦截器失效 excludePathPatterns忽略失效 拦截器失效 spring免验证拦截器不起作用
    查看>>
    Spring Cloud 之注册中心 EurekaServerAutoConfiguration源码分析
    查看>>
    Parrot OS 6.2 重磅发布!推出全新 Docker 容器启动器
    查看>>
    Parrot OS 6.3 发布!全面提升安全性,新增先进工具,带来更高性能
    查看>>
    ParseChat应用源码ios版
    查看>>
    Part 2异常和错误
    查看>>
    Pascal Script
    查看>>
    Spring Boot集成Redis实现keyspace监听 | Spring Cloud 34
    查看>>
    Spring Boot中的自定义事件详解与实战
    查看>>
    Passport 密码模式
    查看>>
    Spring Boot(七十六):集成Redisson实现布隆过滤器(Bloom Filter)
    查看>>
    passport 简易搭配
    查看>>
    passwd命令限制用户密码到期时间
    查看>>
    Spring Boot 动态加载jar包,动态配置太强了!
    查看>>
    Spring @Async执行异步方法的简单使用
    查看>>
    PAT (Basic Level) Practice 乙级1021-1030
    查看>>
    PAT (Basic Level) Practice 乙级1031-1040
    查看>>
    PAT (Basic Level) Practice 乙级1041-1045
    查看>>
    SparkSql的元数据
    查看>>