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

    你可能感兴趣的文章
    Openlayers实战:绘制图形,导出geojson文件
    查看>>
    Openlayers实战:绘制图形,导出KML文件
    查看>>
    Openlayers实战:绘制多边形,导出CSV文件
    查看>>
    Openlayers实战:绘制带箭头的线
    查看>>
    Openlayers实战:绘制点、线、圆、多边形
    查看>>
    Openlayers实战:绘制矩形,正方形,正六边形
    查看>>
    Openlayers实战:自定义放大缩小,显示zoom等级
    查看>>
    Openlayers实战:自定义版权属性信息
    查看>>
    Openlayers实战:输入WKT数据,输出GML、Polyline、GeoJSON格式数据
    查看>>
    Openlayers实战:选择feature,列表滑动,定位到相应的列表位置
    查看>>
    Openlayers实战:非4326,3857的投影
    查看>>
    Openlayers高级交互(1/20): 控制功能综合展示(版权、坐标显示、放缩、比例尺、测量等)
    查看>>
    Openlayers高级交互(10/20):绘制矩形,截取对应部分的地图并保存
    查看>>
    Openlayers高级交互(11/20):显示带箭头的线段轨迹,箭头居中
    查看>>
    Openlayers高级交互(12/20):利用高德逆地理编码,点击位置,显示坐标和地址
    查看>>
    Openlayers高级交互(13/20):选择左右两部分的地图内容,横向卷帘
    查看>>
    Openlayers高级交互(14/20):汽车移动轨迹动画(开始、暂停、结束)
    查看>>
    Openlayers高级交互(15/20):显示海量多边形,10ms加载完成
    查看>>
    Openlayers高级交互(16/20):两个多边形的交集、差集、并集处理
    查看>>
    Openlayers高级交互(17/20):通过坐标显示多边形,计算出最大幅宽
    查看>>