readme

Java面向对象程序设计-完成实验报告(一)

需求分析

本项目将使用装饰模式来实现完成作业的功能。在课程中,教授可能会组织学生讨论一些开放性的问题,然后要求学生根据讨论的结果完成一个报告。这个报告是开放性的,学生可以自由发挥,阐述对问题的看法和观点。但是,报告的结构需要遵守一定的结构和格式。我们将报告的要求总结如下:

  1. 报告需要有一个简短的标题。
  2. 在标题之后,学生需要提供姓名。
  3. 随后是报告的正文。正文的第一个部分是对于问题的阐述。
  4. 在阐明问题之后,学生需要明确的阐明观点。
  5. 上述四个部分(标题、姓名、问题阐述、观点阐述)是报告必须包含的内容。报告还可以包含一些可选的内容。例如,学生可以在报告中包含实验结果,以支持所述的观点。
  6. 学生还可以提供一些数学的证明来支持所述的观点。数学证明也是可选的。

在报告中,标题姓名问题阐述观点阐述必须按照上述顺序出现在报告中。第五点实验结果和第六点数学证明是可选的部分;它们需要在第四点之后出现,但是第五点和第六点之间无顺序要求。

步骤一

本项目中,我们将使用装饰模式建造者模式来实现和生成一个报告对象。首先,我们在com.littlewaterdrop.report包中创建一个新接口ReportGeneratorItf。它有一个成员方法makeReport(),无输入参数,返回一个Report对象。

public interface ReportGeneratorItf {
    Report makeReport();
}

然后,我们在com.littlewaterdrop.report包中实现一个Report类。在本项目中,我们简单地使用一个字符串对象来表示报告的内容。因此,Report类中包含一个String类型的成员变量content。Report类定义了一个构造函数,接收一个字符串类型的参数,表示报告内容。

public class Report {
    private String content = null;

    public Report (String content) {
        this.content = content;
    }
}

步骤二

我们在com.littlewaterdrop.report包中创建一个ReportGenerator类,实现ReportGeneratorItf接口。它包含四个字符串类型的成员变量,分别表示报告的标题(title),学生姓名(name),问题阐述(introduction)和学生的观点阐述(pointOfView)。

这四个成员变量正好对应着报告要求的前四项,并且需要按照顺序出现在报告中。

public class ReportGenerator implements ReportGeneratorItf {
    private String title = null;
    private String name = null;
    private String introduction = null;
    private String pointOfView = null;
    ...
}

我们再在com.littlewaterdrop.report包中创建一个ReportGeneratorDecorator抽象类;它实现了ReportGeneratorItf接口。ReportGeneratorDecorator类中包含一个ReportGeneratorItf类型的成员变量base,用于表示报告中必选部分的内容。

public class ReportGeneratorDecorator implements ReportGeneratorItf {
    private ReportGeneratorItf base = null;
}

ReportGeneratorDecorator类中还新声明了一个方法makeOptionalComponent(),用于生成可选的部分。在makeReport()方法中,首先调用base对象的makeReport()方法,然后再调用makeOptionalComponent()方法。

然后,我们再创建两个新类ReportGeneratorExperimentDecorator和ReportGeneratorProofDecorator。它们继承自ReportGeneratorDecorator,用于为报告新增"装饰",即实验内容或者数学证明。

步骤三

最后,我们在com.littlewaterdrop.report包中创建一个ReportGeneratorBuilder类,用于创建ReportGenerator对象。ReportGeneratorBuilder类包含6个成员函数,分别用于接收上述6个部分的内容。最后,在build()成员函数中返回创建出来的ReportGeneratorItf对象。

ReportGeneratorBuilder withTitle(String title);
ReportGeneratorBuilder withName(String name);
ReportGeneratorBuilder withIntroduction(String introduction);
ReportGeneratorBuilder withPointOfView(String pointOfView);
ReportGeneratorBuilder withExperiment(String experiment);
ReportGeneratorBuilder withProof(String proof);
ReportGeneratorItf build();

我们在使用ReportGeneratorBuilder对象创建Report时,可以按照如下方式。

Report report = ReportGenerator.builder()
                               .withTitle("A Title")
                               .withName("A Student's Name")
                               .withIntroduction("An Introduction")
                               .withPointOfView("A Point of View")
                               .withProof("A Proof")
                               .build()
                               .makeReport()
                               ;

参考文档

  1. Java编程语言的基本概念
  2. 设计模式
  3. Maven工程管理工具
Copyright  2019 Little Waterdrop, LLC. All Rights Reserved.