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

    你可能感兴趣的文章
    Objective-C实现cycle sort循环排序算法(附完整源码)
    查看>>
    Objective-C实现data transformations数据转换算法(附完整源码)
    查看>>
    Objective-C实现datamatrix二维码识别 (附完整源码)
    查看>>
    Objective-C实现DateToDay 方法算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>
    Objective-C实现DBSCAN聚类算法(附完整源码)
    查看>>
    Objective-C实现decision tree决策树算法(附完整源码)
    查看>>
    Objective-C实现degreeToRadian度到弧度算法(附完整源码)
    查看>>
    Objective-C实现depth first search深度优先搜索算法(附完整源码)
    查看>>
    Objective-C实现DES和3DES加解密算法(附完整源码)
    查看>>
    Objective-C实现des文件加密算法(附完整源码)
    查看>>
    Objective-C实现detectDirectedCycle检测定向循环算法(附完整源码)
    查看>>
    Objective-C实现detectUndirectedCycle检测无向循环算法(附完整源码)
    查看>>
    Objective-C实现deutsch jozsa算法(附完整源码)
    查看>>
    Objective-C实现DFS判断是否是二分图Bipartite算法(附完整源码)
    查看>>
    Objective-C实现DFS遍历或搜索图数据结构算法(附完整源码)
    查看>>
    Objective-C实现Diffie-Hellman算法(附完整源码)
    查看>>
    Objective-C实现Diffie—Hellman密钥交换(附完整源码)
    查看>>
    Objective-C实现Diffie—Hellman密钥交换(附完整源码)
    查看>>
    Objective-C实现Dijkstra最小路径算法(附完整源码)
    查看>>