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

    你可能感兴趣的文章
    Numpy 入门
    查看>>
    NumPy 库详细介绍-ChatGPT4o作答
    查看>>
    NumPy 或 Pandas:将数组类型保持为整数,同时具有 NaN 值
    查看>>
    numpy 或 scipy 有哪些可能的计算可以返回 NaN?
    查看>>
    numpy 数组 dtype 在 Windows 10 64 位机器中默认为 int32
    查看>>
    numpy 数组与矩阵的乘法理解
    查看>>
    NumPy 数组拼接方法-ChatGPT4o作答
    查看>>
    numpy 用法
    查看>>
    Numpy 科学计算库详解
    查看>>
    Numpy.fft.fft和numpy.fft.fftfreq有什么不同
    查看>>
    numpy.linalg.norm(求范数)
    查看>>
    Numpy.ndarray对象不可调用
    查看>>
    Numpy.VisibleDeproationWarning:从不整齐的嵌套序列创建ndarray
    查看>>
    Numpy:按多个条件过滤行?
    查看>>
    Numpy:条件总和
    查看>>
    numpy、cv2等操作图片基本操作
    查看>>
    numpy中的argsort的用法
    查看>>
    NumPy中的精度:比较数字时的问题
    查看>>
    numpy判断对应位置是否相等,all、any的使用
    查看>>
    Numpy多项式.Polynomial.fit()给出的系数与多项式.Polyfit()不同
    查看>>