博客
关于我
设计模式-装饰模式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/

    你可能感兴趣的文章
    Pandas库函数
    查看>>
    Pandas库常用方法、函数集合
    查看>>
    Pandas循环提速 7 万多倍是怎么实现的?
    查看>>
    pandas打乱数据的顺序
    查看>>
    pandas指定列数据归一化
    查看>>
    pandas改变一列值(通过apply)
    查看>>
    Pandas数据分析的环境准备
    查看>>
    Pandas数据可视化怎么做?用实战案例告诉你!
    查看>>
    Pandas数据处理与分析教程:从基础到实战
    查看>>
    Pandas数据结构之DataFrame常见操作
    查看>>
    pandas整合多份csv文件
    查看>>
    pandas某一列转数组list
    查看>>
    Pandas模块,我觉得掌握这些就够用了!
    查看>>
    Pandas玩转文本处理!
    查看>>
    SpringBoot 整合 Mybatis Plus 实现基本CRUD功能
    查看>>
    pandas的to_sql方法中使用if_exists=‘replace‘
    查看>>
    Springboot ppt转pdf——aspose方式
    查看>>
    pandas读取csv编码utf-8报错
    查看>>
    pandas读取parquet报错
    查看>>
    pandas读取数据用来深度学习
    查看>>