设计模式-装饰者模式在Java中的使用示例

场景

装饰者模式(Decorator Pattern)

是指在不改变原有对象的基础上,将功能附加到对象上,提供了比继承更有弹性的方案。

适用场景:

1、扩展一个类的功能或给一个类添加职责。

2、动态给一个对象添加功能,这些功能可以再动态地撤销。

3、生活中的应用比如给房子装修、给煎饼果子加肠、给蛋糕加巧克力。

注:

博客:
霸道流氓气质的博客_CSDN博客-C#,架构之路,SpringBoot领域博主
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

装饰者模式示例-购买电脑加装内存条和硬盘

创建一个电脑类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class Computer {
    protected String getMsg(){
        return "电脑";
    }
    public int getPrice(){
        return 5000;
    }
}

然后创建一个加内存条的电脑类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class ComputerWithMemory extends Computer{
    @Override
    protected String getMsg() {
        return super.getMsg()+"一个内存条";
    }

    @Override
    public int getPrice() {
        return super.getPrice()+1000;
    }
}

再创建一个既加内存条又加硬盘的电脑类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class ComputerWithMemoryAndDisk extends ComputerWithMemory{
    @Override
    protected String getMsg() {
        return super.getMsg()+"一个硬盘";
    }

    @Override
    public int getPrice() {
        return super.getPrice()+500;
    }
}

编写客户端测试代码

package com.ruoyi.demo.designPattern.decoratorPattern;

public class ComputerTest {
    public static void main(String[] args) {
        Computer computer = new Computer();
        System.out.println(computer.getMsg()+",总价格:"+computer.getPrice());

        ComputerWithMemory computerWithMemory = new ComputerWithMemory();
        System.out.println(computerWithMemory.getMsg()+",总价格:"+computerWithMemory.getPrice());

        ComputerWithMemoryAndDisk computerWithMemoryAndDisk = new ComputerWithMemoryAndDisk();
        System.out.println(computerWithMemoryAndDisk.getMsg()+",总价格:"+computerWithMemoryAndDisk.getPrice());
    }
}

但是如果用户需要一个加两个内存条、加一个硬盘的电脑,用现在的类是创建不出来的,

也无法自动计算出价格,但是也不能一直通过做定制类来解决。

下面通过装饰者模式来解决

创建一个电脑的抽象类

package com.ruoyi.demo.designPattern.decoratorPattern;

public abstract class AbstarctComputer {
    protected abstract String getMsg();
    protected abstract int getPrice();
}

然后创建一个基本电脑套餐类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class BaseComputer extends AbstarctComputer{
    @Override
    protected String getMsg() {
        return "电脑";
    }

    @Override
    protected int getPrice() {
        return 5000;
    }
}

再创建一个扩展套餐的抽象装饰者类

package com.ruoyi.demo.designPattern.decoratorPattern;

public abstract class ComputerDecotator extends AbstarctComputer{

    //静态代理,委派
    private AbstarctComputer computer;

    public ComputerDecotator(AbstarctComputer computer){
        this.computer = computer;
    }

    protected abstract void doSomething();

    @Override
    protected String getMsg() {
        return this.computer.getMsg();
    }

    @Override
    public int getPrice() {
        return this.computer.getPrice();
    }
}

然后创建内存条装饰者类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class MemoryDecorator extends ComputerDecotator{

    public MemoryDecorator(AbstarctComputer computer) {
        super(computer);
    }

    @Override
    protected void doSomething() {

    }

    @Override
    protected String getMsg() {
        return super.getMsg()+"+1个内存条";
    }

    @Override
    public int getPrice() {
        return super.getPrice()+1000;
    }
}

再创建硬盘装饰者类

package com.ruoyi.demo.designPattern.decoratorPattern;

public class DiskDecorator extends ComputerDecotator{

    public DiskDecorator(AbstarctComputer computer) {
        super(computer);
    }

    @Override
    protected void doSomething() {

    }

    @Override
    protected String getMsg() {
        return super.getMsg()+"+1个硬盘";
    }

    @Override
    public int getPrice() {
        return super.getPrice()+500;
    }
}

客户端测试代码

package com.ruoyi.demo.designPattern.decoratorPattern;

public class ComputerDecoratorTest {

    public static void main(String[] args) {
        AbstarctComputer computer;
        //要买1台电脑
        computer = new BaseComputer();
        //加一个内存条
        computer = new MemoryDecorator(computer);
        //加一个硬盘
        computer = new DiskDecorator(computer);
        //再加一个内存条
        computer = new MemoryDecorator(computer);

        System.out.println(computer.getMsg()+",总价:"+computer.getPrice());
    }
}

 

装饰者模式的优缺点

优点:

1、装饰者模式是继承的有力补充,且比继承灵活,可以在不改变原有对象的情况下动态地给

一个对象扩展功能,即插即用。

2、使用不同的装饰类及这些装饰类的排列组合,可以实现不同的效果。

3、装饰者模式完全符合开闭原则。

缺点:

1、会出现更多的代码、更多的类,增加程序的复杂性。

2、动态装饰时,多层装饰会更复杂。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java设计模式是一种在软件开发常用的解决方案,它提供了一套经过验证的方法来解决特定问题。简单工厂模式是设计模式的一种常见模式,它主要用于根据客户端的需求动态创建对象,隐藏对象的创建细节。简单工厂模式的应用场景包括以下几种: 1. 当需要根据给定的条件动态创建不同类型的对象时,可以使用简单工厂模式。例如,根据用户的选择创建不同类型的图形对象。 2. 当对象的创建逻辑复杂且需要集管理时,可以使用简单工厂模式。通过将对象的创建逻辑封装在工厂类,可以提高代码的可维护性和可扩展性。 下面是一个简单工厂模式的代码示例: ```java // 抽象产品类 interface Product { void operation(); } // 具体产品类A class ConcreteProductA implements Product { @Override public void operation() { System.out.println("ConcreteProductA operation"); } } // 具体产品类B class ConcreteProductB implements Product { @Override public void operation() { System.out.println("ConcreteProductB operation"); } } // 简单工厂类 class SimpleFactory { public static Product createProduct(String type) { if (type.equals("A")) { return new ConcreteProductA(); } else if (type.equals("B")) { return new ConcreteProductB(); } return null; } } // 客户端 public class Client { public static void main(String[] args) { Product productA = SimpleFactory.createProduct("A"); productA.operation(); // 输出:ConcreteProductA operation Product productB = SimpleFactory.createProduct("B"); productB.operation(); // 输出:ConcreteProductB operation } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霸道流氓气质

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值