设计模式-装饰者模式在Java中使用实例-打印发票装饰抬头和脚注

news/2024/7/24 7:29:57 标签: 设计模式, java, python

场景

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

设计模式-装饰者模式在Java中的使用示例_java装饰者模式例子-CSDN博客

上面装饰器的调用示例如下

        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());

下面记录一个它的变形,以软件设计师2016年下半年试题六为例

某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,

得到如图6-1所示的类图

注:

博客:
霸道流氓气质-CSDN博客

实现

1、新建发票正文类

public class Invoice {
    public void printInvoice(){
        System.out.println("This is the content of the invoice !");
    }
}

2、新建普通装饰器

public class Decorator extends Invoice{
    protected Invoice ticket;
    public Decorator(Invoice t){
        ticket = t;
    }

    public void printInvoice(){
        if(ticket !=null){
            ticket.printInvoice();
        }
    }
}

3、新建抬头装饰器

public class HeadDecorator extends Decorator{
    public HeadDecorator(Invoice t) {
        super(t);
    }

    public void printInvoice(){
        System.out.println("This is the header of the invoice!");
        super.printInvoice();
    }
}

4、新建脚注装饰器

public class FootDecorator extends Decorator{
    public FootDecorator(Invoice t) {
        super(t);
    }
    public void printInvoice(){
        super.printInvoice();
        System.out.println("This is the footnote of the invoice !");
    }
}

5、调用示例

        Invoice t = new Invoice();
        Invoice ticket;
        ticket = new HeadDecorator(new FootDecorator(t));
        ticket.printInvoice();
        System.out.println("----------------------------");
        ticket = new HeadDecorator(new FootDecorator(null));
        ticket.printInvoice();

以上调用输出结果

This is the header of the invoice!
This is the content of the invoice !
This is the footnote of the invoice !
----------------------------
This is the header of the invoice!
This is the footnote of the invoice !

6、分析

基础回顾

子类创建对象时,会先调用父类的构造方法,然后再调用子类自己的构造方法。
但是,如果子类没有显式地定义构造方法,会使用默认的无参构造方法。

这就是为什么有时候我们需要重写继承的构造方法的原因。
 
在继承中,子类可以通过super关键字调用父类的构造方法。如果子类没有使用super关键字调用父类的构造方法,

Java会自动调用父类的无参构造方法。如果父类没有提供无参构造方法,且子类没有显示地调用其他构造方法,会导致编译错误
 
在父类中只有 有参构造函数,没有无参构造函数,子类如果不写构造函数,系统默认创建一个无参构造函数,

子类这个无参的构造函数就会去调用父类的无参构造函数,时候就出错。

以上链式调用时

        ticket = new HeadDecorator(new FootDecorator(t));
        ticket.printInvoice();

不好理解,可以将其修改为如下便于理解

        Invoice t = new Invoice();
        t.printInvoice();
        FootDecorator footDecorator = new FootDecorator(t);
        footDecorator.printInvoice();
        Invoice ticket = new HeadDecorator(footDecorator);
        ticket.printInvoice();

实现过程分步解析

首先是FootDecorator footDecorator = new FootDecorator(t)

先调用FootDecorator的构造方法,将t(设定地址为Invoice@506)通过构造方法传参传递

并在FootDecorator的构造方法中调用super(t)将其传到父类Decorator的构造方法中进行

 protected Invoice ticket的属性的赋值。此时父类Decorator的ticker为Invoice@506

然后将上面新建的footDecorator(设定地址为FootDecorator@509)传递给HeadDecorator的构造方法

此时Invoice t为FootDecorator@509,在其构造方法中通过super(t)调用父类构造方法,并将其赋值给

父类Decorator的protected Invoice ticket,所以此时父类Decorator的ticket为FootDecorator@509

返回值为HeadDecorator的对象(设定为HeadDecorator@511)

然后调用HeadDecorator@511的printInvoice方法

在printInvoice方法中,先输出

This is the header of the invoice!

然后调用父类Decorator的printInvoice方法

此时父类Decorator的ticket为FootDecorator@509,不为空,所以会调用FootDecorator对象FootDecorator@509

的printInvoice方法,方法中会先调用其父类的printInvoice方法,FootDecorator@509父类对象的ticket为Invoice@506

Invoice@506调用printInvoice方法会输出

This is the content of the invoice !

然后FootDecorator@509的printInvoice方法继续输出

This is the footnote of the invoice !


http://www.niftyadmin.cn/n/5452325.html

相关文章

mysql字段多个值,mybatis/mybatis-plus匹配查询

mysql中有一个字段是字符串类型的,category字段值有多个用逗号分割的,例如:娱乐,时尚美妆,美食 。现在想实现这么一个功能, 前端传参 字符串,美食,娱乐。现在想在mybatis的xml中实现,查询,能查到…

详解:写作和赚钱的 4 个关系!看完你一定会忍不住想开始写!

飞书文档的加密很强,也没有和自家的豆包大模型融合,所以只能通过其他方式获取文档的内容。 (1)将飞书文档转换为PDF,要用到浏览器插件: GoFullPage - Full Page Screen Capture - Microsoft Edge Addons …

C语言_第一轮笔记_指针

8.1 密码开锁 地址和指针 一般以变量所在的内存单元的第一个字节的地址作为他的地址NULL的值为0,代表空指针 指针变量的定义 类型名 *指针变量名类型名指定指针变量所指向变量的类型指针声明符*在定义指针变量时被使用,说明被定义的那个变量是指针指针变…

Golang 环境变量配置 mockgen安装(macOS系统)

1、将 $GOPATH/bin 添加到环境变量中 查看当前的 GOPATH 地址: go env GOPATH 将 GOPATH/bin 地址添加到系统环境变量里: 打开 zsh 终端文件 sudo vim ~/.zshrc 添加此路径到 zshrc 文件最后一行 export PATH$PATH:${GOPATH}/bin 2、安装 mockge…

Android获取经纬度的最佳实现方式

Android中获取定位信息的方式有很多种,系统自带的LocationManager,以及第三方厂商提供的一些定位sdk,都能帮助我们获取当前经纬度,但第三方厂商一般都需要申请相关的key,且调用量高时,还会产生资费问题。这…

Leetcode 453. 最小操作次数使数组元素相等

原题链接:Leetcode 453. minimum moves to equal array elements Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. In one move, you can increment n - 1 elements of the array by 1. …

第十三届蓝桥杯省赛真题 Java C 组【原卷】

文章目录 发现宝藏【考生须知】试题 A: 排列字母试题 B: 特殊时间试题 C: 纸张尺寸试题 D: 求和试题 E : \mathbf{E}: E: 矩形拼接试题 F: 选数异或试题 G: GCD试题 H: 青蛙过河试题 I: 因数平方和试题 J \mathrm{J} J : 最长不下降子序列 发现宝藏 前些天发现了一个巨牛的人…

C++ STL - 优先级队列及其模拟实现

目录 0. 引言 1. priority_queue 介绍 1.1 构造函数 1.2 priority_queue 接口函数使用 1.3 仿函数 1.4 题目练习 2. priority_queue 模拟实现 2.1基本框架: 2.2 默认构造函数 2.3 基本函数 2.4 堆的向上以及向下调整 0. 引言 优先队列 (priority_queu…