设计模式-装饰模式

介绍

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。
继承机制同样可以给现有类增加功能,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法。但是这种方法是静态的,用户不能控制增加行为的方式和时机。
而装饰器模式是将一个类的对象嵌入另一个对象中,由另一个对象来决定是否调用嵌入对象的行为以便扩展自己的行为。

结构图

这里写图片描述

时序图

这里写图片描述

案例

我这边就用街边小吃来举例子。客人可以选择面条、米粉等,然后可以在面条或者米粉上添加其他一些小吃,如火腿、丸子等等。

食物接口

public interface Food {
    void getDescription();
    int getMoney();
}

面条主食

public class NoodleFood implements Food{

    @Override
    public void getDescription() {
        System.out.print("-面条-");
    }

    @Override
    public int getMoney() {
        return 4;
    }

}

米粉主食

public class FlourFood implements Food{

    @Override
    public void getDescription() {
        System.out.print("-米粉-");
    }

    @Override
    public int getMoney() {
        return 5;
    }

}

装饰接口

public interface CondimentDecorator extends Food{

}

丸子装饰类

public class BallDecorator implements CondimentDecorator{
    private Food food;


    public BallDecorator(Food food) {
        this.food = food;
    }

    @Override
    public void getDescription() {
        food.getDescription();
        System.out.print("-丸子-");
    }

    @Override
    public int getMoney() {
        return food.getMoney()+3;
    }

}

火腿装饰类

public class HamDecorator implements CondimentDecorator{
    private Food food;

    public HamDecorator(Food food) {
        this.food = food;
    }

    @Override
    public void getDescription() {
        food.getDescription();
        System.out.print("-火腿-");
    }

    @Override
    public int getMoney() {
        return food.getMoney()+3;
    }

}

测试类

public class Client {
    public static void main(String[] args) {

        Food food = new HamDecorator(new BallDecorator(new HamDecorator(new NoodleFood())));
        food.getDescription();
        System.out.println();
        System.out.println(food.getMoney());

        Food food2 = new BallDecorator(new HamDecorator(new BallDecorator(new FlourFood())));
        food2.getDescription();
        System.out.println();
        System.out.println(food2.getMoney());
    }
}

测试结果
这里写图片描述

总结

使用装饰模式来实现扩展比继承更加灵活,它以对客户透明的方式动态地给一个对象附加更多的责任。如果使用继承会照成类爆炸。而且继承是一种耦合度较大的静态关系,无法在程序运行时动态扩展。
装饰模式包含四个角色:抽象构件定义了对象的接口,可以给这些对 象动态增加职责(方法);具体构件定义了具体的构件对象,实现了 在抽象构件中声明的方法,装饰器可以给它增加额外的职责(方法); 抽象装饰类是抽象构件类的子类,用于给具体构件增加职责,但是具 体职责在其子类中实现;具体装饰类是抽象装饰类的子类,负责向构 件添加新的职责。

在jdk中最典型的就是I/O了,感兴趣的可以去了解io的源码。
这里写图片描述

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(“1.txt”)));

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值