装饰者模式

引言

还是以热干面,加鸡蛋的热干面,加牛肉的热干面举例子

代码如下

1.先创建一个面条的接口

public interface Noodle {

    public String getNoodleName();

    public Double getNoodlePrice();
}

写一些具体的类去实现面条,比如说热干面

public class HotDryNoodle implements Noodle{

    @Override
    public String getNoodleName() {
        return "热干面";
    }

    @Override
    public Double getNoodlePrice() {
        return 4.0;
    }

}

然后此时写一个面条的装饰器,这个装饰器应该实现有参的构造方法,传入什么面条就行,在面条的基础上加东西。

public abstract class NoodleDecorator implements Noodle{

    public Noodle noodle;

    public NoodleDecorator(Noodle noodle) {
        this.noodle = noodle;
    }

}

现在可以写加鸡蛋的热干面,加牛肉的热干面了

public class AddBeafNoodle extends NoodleDecorator {

    public AddBeafNoodle(Noodle noodle) {
        super(noodle);
    }

    @Override
    public String getNoodleName() {
        return noodle.getNoodleName()+"加牛肉";
    }

    @Override
    public Double getNoodlePrice() {
        return noodle.getNoodlePrice()+5.0;
    }

}
public class AddEggNoodle extends NoodleDecorator{

    public AddEggNoodle(Noodle noodle) {
        super(noodle);
    }

    @Override
    public String getNoodleName() {
        return noodle.getNoodleName()+"加鸡蛋";
    }

    @Override
    public Double getNoodlePrice() {
        return noodle.getNoodlePrice()+2.0;
    }

}

测试

public class TestMain {

    public static void main(String[] args) {
        HotDryNoodle hotDryNoodle = new HotDryNoodle();

        AddEggNoodle eggNoodle = new AddEggNoodle(hotDryNoodle);
        System.out.println(eggNoodle.getNoodleName());
        System.out.println(eggNoodle.getNoodlePrice());

        AddBeafNoodle beafNoodle = new AddBeafNoodle(hotDryNoodle);
        System.out.println(beafNoodle.getNoodleName());
        System.out.println(beafNoodle.getNoodlePrice());

        AddBeafNoodle beafAndEggNoodle = new AddBeafNoodle(new AddEggNoodle(hotDryNoodle));
        System.out.println(beafAndEggNoodle.getNoodleName());
        System.out.println(beafAndEggNoodle.getNoodlePrice());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
装饰者模式(Decorator Pattern)的类图包含以下几个角色: - Component:抽象构件角色,定义一个抽象接口,用来规范具体构件角色和装饰角色的行为。 - ConcreteComponent:具体构件角色,实现 Component 接口,或者继承抽象类 Component,并实现其方法。 - Decorator:抽象装饰角色,继承或实现 Component 接口,包含一个指向 Component 对象的引用,并定义一个与 Component 接口一致的接口。 - ConcreteDecorator:具体装饰角色,继承或实现 Decorator 类,包含一个指向 Component 对象的引用,并可以增加一些额外的功能。 装饰者模式的类图示例如下: ``` +-------------------------+ | Component | +-------------------------+ | +operation() | +-------------------------+ /\ | +-------------------------+ | ConcreteComponent | +-------------------------+ | +operation() | +-------------------------+ /\ | | +-------------------------+ | Decorator | +-------------------------+ | -component: Component | +-------------------------+ /\ | | +-------------------------+ | ConcreteDecorator | +-------------------------+ | -component: Component | | +operation() | +-------------------------+ ``` 在上面的类图中,Component 是抽象构件角色,定义了一个抽象接口 operation(),ConcreteComponent 是具体构件角色,实现了 Component 接口,并且定义了具体的业务方法。Decorator 是抽象装饰角色,继承或实现了 Component 接口,包含一个指向 Component 对象的引用,并定义了一个与 Component 接口一致的接口。ConcreteDecorator 是具体装饰角色,继承或实现了 Decorator 类,包含一个指向 Component 对象的引用,并可以增加一些额外的功能。 在装饰者模式中,客户端可以使用一个或多个装饰器来动态地改变一个对象的行为,而不需要修改原始对象的代码。通过将对象包装在一系列装饰器中,可以在运行时动态地添加或删除功能,从而实现更灵活、更可扩展的设计。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值