装饰者设计模式知多少

目录

目标

概述

实现


目标

熟悉装饰者设计模式,了解装饰者设计模式的使用场景、具体实现。


概述

一、结构型设计模式

结构型设计模式是设计模式的一种类型。它的关注点在于如何将对象和类组合成更大的结构。较为常见的结构型设计模式如:装饰者设计模式、代理模式、适配器模式、桥接模式、组合模式等。

二、装饰者设计模式

属于结构型设计模式。核心思想是将对象包装在一个装饰者对象中,使得对象有了更多的行为。

优点:

  • 允许运行时动态地添加对象,实现灵活扩展功能。
  • 符合开放封闭原则,实现新增功能而不修改代码。
  • 避免使用子类来扩展对象的功能,从而避免产生过多子类。

缺点:

  • 该模式会产生许多小对象。
  • 装饰的过程需要注意顺序。
  • 动态地创建和组合对象会增加系统运行时地开销。

三、使用场景

  • 不改变对象的结构又想为对象新增其他行为。
  • 涉及到多种功能之间的组合。
  • 需要在运行时才能确定对象之间的组合关系。

四、列举装饰者模式在成熟的框架中的应用

  • Netty中的ChannelPipeline就是由ChannelHandler装饰而成。
  • Spring AOP在业务方法执行的前后添加日志、事务管理的逻辑。

实现

需求

旅游公司推出了两款增值套餐,可以自由搭配组合。两款套餐分别是:WIFI服务、接送机服务。现在需要根据用户的选择,动态地生成旅游费用。

基本旅游套餐实体类

package com.ctx.decorator;

import java.math.BigDecimal;

// 具体旅游路线类
public class ConcreteTravelRoute extends TravelRoute {
    public ConcreteTravelRoute() {
        description = "经典旅游路线";
    }

    @Override
    public BigDecimal getPrice() {
        return new BigDecimal(1000.0);
    }
}

基本旅游套餐抽象类

package com.ctx.decorator;

import java.math.BigDecimal;

// 抽象旅游路线类
public abstract class TravelRoute {
    protected String description;  // 路线描述

    public String getDescription() {
        return description;
    }

    public abstract BigDecimal getPrice();  // 获取路线价格

}

基本旅游套餐服务类

package com.ctx.decorator;

// 抽象旅游服务类(装饰者)
public abstract class TravelService extends TravelRoute {
    public abstract String getDescription();  // 获取服务描述
}

增值套餐类

package com.ctx.decorator.decorators;

import com.ctx.decorator.TravelRoute;
import com.ctx.decorator.TravelService;

import java.math.BigDecimal;

// 接送机服务类(具体装饰者)
public class AirportTransferService extends TravelService {
    private TravelRoute travelRoute;

    public AirportTransferService(TravelRoute travelRoute) {
        this.travelRoute = travelRoute;
    }

    @Override
    public String getDescription() {
        return travelRoute.getDescription() + " + 接送机服务";
    }

    @Override
    public BigDecimal getPrice() {
        return travelRoute.getPrice().add(new BigDecimal(100.0));
    }
}
package com.ctx.decorator.decorators;

import com.ctx.decorator.TravelRoute;
import com.ctx.decorator.TravelService;

import java.math.BigDecimal;

// WiFi服务类(具体装饰者)
public class WiFiService extends TravelService {
    private TravelRoute travelRoute;

    public WiFiService(TravelRoute travelRoute) {
        this.travelRoute = travelRoute;
    }

    @Override
    public String getDescription() {
        return travelRoute.getDescription() + " + WiFi服务";
    }

    @Override
    public BigDecimal getPrice() {
        return travelRoute.getPrice().add(new BigDecimal( 50.0));
    }
}

测试调用

package com.ctx.decorator;

import com.ctx.decorator.decorators.AirportTransferService;
import com.ctx.decorator.decorators.WiFiService;

/**
 * 在运行时动态地将责任附加到对象上,是典型的动态装饰。
 */
public class Test {
    public static void main(String[] args) {
        // 创建一个经典旅游路线
        TravelRoute travelRoute = new ConcreteTravelRoute();
        // 打印路线描述和价格
        System.out.println(travelRoute.getDescription());
        System.out.println("价格:" + travelRoute.getPrice() + "元");

        // 添加WiFi服务和接送机服务
        travelRoute = new WiFiService(travelRoute);
        // 打印路线描述和价格
        System.out.println(travelRoute.getDescription());
        System.out.println("价格:" + travelRoute.getPrice() + "元");

        travelRoute = new AirportTransferService(travelRoute);
        // 打印路线描述和价格
        System.out.println(travelRoute.getDescription());
        System.out.println("价格:" + travelRoute.getPrice() + "元");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值