首页 > 其他分享 >[设计模式]装饰者模式

[设计模式]装饰者模式

时间:2024-08-09 17:09:16浏览次数:6  
标签:fastFood getPrice return getDesc 模式 public Override 设计模式 装饰

抽象构件

public abstract class FastFood {

    public String desc;
    public int price;

    public abstract String getDesc();

    public abstract int getPrice();

}

具体构件

米饭

public class Rice extends FastFood {

    public Rice() {
        this.desc = "米饭";
        this.price = 10;
    }

    @Override
    public String getDesc() {
        return this.desc;
    }

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

面条

public class Noddle extends FastFood {

    public Noddle() {
        this.desc = "面条";
        this.price = 8;
    }

    @Override
    public String getDesc() {
        return this.desc;
    }

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

抽象装饰器

public abstract class SideFood extends FastFood {

    FastFood fastFood;

    public SideFood(FastFood fd) {
        this.fastFood = fd;
    }

    @Override
    public String getDesc() {
        return this.fastFood.getDesc();
    }

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

具体装饰器

鸡蛋

public class Egg extends SideFood {
    public Egg(FastFood fd) {
        super(fd);
    }

    @Override
    public String getDesc() {
        return this.fastFood.getDesc() + "加鸡蛋";
    }

    @Override
    public int getPrice() {
        return this.fastFood.getPrice() + 5;
    }
}

火腿

public class Ham extends SideFood {
    public Ham(FastFood fd) {
        super(fd);
    }

    @Override
    public String getDesc() {
        return this.fastFood.getDesc() + "加火腿";
    }

    @Override
    public int getPrice() {
        return this.fastFood.getPrice() + 8;
    }
}

使用案例

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

        Rice rice = new Rice();
        Egg egg = new Egg(rice);
        Ham ham = new Ham(egg);

        System.out.println(ham.getDesc());
        System.out.println(ham.getPrice());

    }
}

类图

image

标签:fastFood,getPrice,return,getDesc,模式,public,Override,设计模式,装饰
From: https://www.cnblogs.com/DCFV/p/18350997

相关文章

  • “斯诺克”不等于“台球”-《分析模式》漫谈17
    DDD领域驱动设计批评文集做强化自测题获得“软件方法建模师”称号《软件方法》各章合集“AnalysisPatterns”的第一章有这么一句:Considersomeonewhowantstowritesoftwaretosimulatea game of snooker. 2004(机械工业出版社)中译本的译文为: game翻译成“游......
  • K8S中,flannel有几种模式?
    在Kubernetes(K8S)中,Flannel作为一个流行的容器网络接口(CNI)插件,用于为集群中的容器提供网络互通能力。Flannel支持多种模式来实现其网络功能,主要包括以下几种常见模式:1.VXLAN模式描述:VXLAN(VirtualExtensibleLAN)是Flannel的默认后端驱动,它使用VXLAN封装技术来创建跨节点的虚拟......
  • 基于java+springboot+vue基于MVC模式的考研论坛交流管理系统的设计与实现万字文档和PP
    前言......
  • 策略模式揭秘:如何让飞书、企业微信、钉钉的入职与生日祝福更智能?
    继上一篇飞书、企业微信、钉钉如何精准推送入职与生日祝福背后的数据魔法之后,今天在此基础上分享下策略模式。策略模式是一种行为型设计模式,在工作中使用的频次非常高。生日祝福,入职周年祝福等,每一种都是一种不同的策略。不了解背景的人可以先去看看入职周年祝福与生日......
  • Java设计模式和AOP编程
    Java六大设计原则;Java23种设计模式(在此介绍三种设计模式)Java设计模式单例模式应用场景:spring中bean的作用域用的就是单例模式//基本的单例模式————懒汉式publicclassstudent{//3.创建static修饰的成员变量privatestaticstudentstu;//1.设计私......
  • C# 设计模式之模板方法模式
    总目录前言在日常的工作中,有时候我们做PPT,做合同,做简历,如果我们自己从头去写这些文档,不免有些太过耗时耗力;大多时候都是去找相关的PPT模板,合同模板,简历模板,拿过来直接用。为什么可以使用模板,因此这些资料大部分的信息和信息框架都是一致的,我们只需要将自己差异化的内容填......
  • C# 设计模式之代理模式
    总目录前言其实代理模式,在生活中无处不在;就比如租房,一般都是通过中介或者第三方的App去租房子(此处默认他们都是诚信友好的哈),那么为什么我们不自己找房子呢?租过房子应该都知道,自己找房子太麻烦了,既要找原房东,又要搞合同,出了问题扯皮也扯不清等等一些问题,主要是费时费力。......
  • Java设计模式—责任链模式(Chin of Responsibility)
    目录引言1.职责链设计模式简介1.1定义1.2解决的问题2.设计模式的结构2.1类图2.2示例代码3.优点4.缺点5.实际应用5.1SpringAOP5.2JavaServletFilter5.3ReactorPattern5.4Java中的日志记录库6.结论注意事项引言在软件开发中,设计模式是一......
  • calico三种网络模式
    calico几种模式对比注意calico不会有任何网桥会为每一个容器创建一个Vethpair设备,一端在容器内,一端设置到宿主机上数据的转发,靠Calico维护的路由规则模式数据包封包优点缺点vxlan封包,在vxlan设备上将pod发来的数据包源、目的mac替换为本机vxlan网卡和对端节点vx......
  • 基于领航-跟随模式的无人车编队仿真与导航
    基于领航-跟随模式的无人车编队仿真与导航1.查看系统环境要运行本仿真程序,需要保证当前环境为ubuntu18.04+ros-melodic-desktop-full查看ubuntu版本:rosnoetic@rosnoetic-VirtualBox:~$lsb_release-aNoLSBmodulesareavailable.DistributorID: UbuntuDescription: ......