Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 特定上下文中的继承与组合_Oop_Inheritance_Composition - Fatal编程技术网

Oop 特定上下文中的继承与组合

Oop 特定上下文中的继承与组合,oop,inheritance,composition,Oop,Inheritance,Composition,对于使用垂直继承或组合实现一个简单的OOP实现,我正在重新考虑。我读了很多书,但我仍然不善于下决心^^ 我需要制作四种不同的报告:报价、发票、目录和手册: -每个报表都有相同的页眉和页脚。 -报价单和发票包含具有不同数据的相同格式表。 -小册子和目录与其他报告的结构不同,没有表格 我想在这里得到关于OOP设计的帮助;我将介绍我的两个想法伪代码,但任何反馈都将不胜感激 继承 以下是表格功能的组合 非常感谢 这可能是一个宗教问题。任何一种方法都应该有效,而且从一种方法重构到另一种方法都很容易。标准的

对于使用垂直继承或组合实现一个简单的OOP实现,我正在重新考虑。我读了很多书,但我仍然不善于下决心^^

我需要制作四种不同的报告:报价、发票、目录和手册: -每个报表都有相同的页眉和页脚。 -报价单和发票包含具有不同数据的相同格式表。 -小册子和目录与其他报告的结构不同,没有表格

我想在这里得到关于OOP设计的帮助;我将介绍我的两个想法伪代码,但任何反馈都将不胜感激

继承

以下是表格功能的组合


非常感谢

这可能是一个宗教问题。任何一种方法都应该有效,而且从一种方法重构到另一种方法都很容易。标准的逻辑是倾向于组合而不是继承,但是选择感觉正确的

class Report
{
    method Header()
    {
        // Print the header
    }

    method Footer()
    {
        // Print the footer
    }
}

class ReportTable
{
    method Table()
    {
        // Print the table
    }
}

class Quote extends ReportTable
{
    method BuildReport()
    {
        call Header()

        call Table()
        // Print some more quote stuff

        call Footer()
    }
}

class Invoice extends ReportTable
{
    method BuildReport()
    {
        call Header()

        call Table()
        // Print some more invoice stuff

        call Footer()
    }
}

class Catalogue extends Report
{
    method BuildReport()
    {
        call Header()

        // Print catalogue stuff

        call Footer()
    }
}

class Brochure extends Report
{
    method BuildReport()
    {
        call Header()

        // Print brochure stuff

        call Footer()
    }
}
    class Report
{
    method Header()
    {
        // Print the header
    }

    method Footer()
    {
        // Print the footer
    }
}

class Table
{
    method Table()
    {
        // Print the table
    }
}

class Quote extends Report
{
    property table

    constructor Quote( Table table )
    {
        self.table = table
    }

    method BuildReport()
    {
        call Header()

        call self.table.Table()
        // Print some more quote stuff

        call Footer()
    }
}

class Invoice extends Report
{
    property table

    constructor Invoice( Table table )
    {
        self.table = table
    }

    method BuildReport()
    {
        call Header()

        call self.table.Table()
        // Print some more invoice stuff

        call Footer()
    }
}

class Catalogue extends Report
{
    method BuildReport()
    {
        call Header()

        // Print catalogue stuff

        call Footer()
    }
}

class Brochure extends Report
{
    method BuildReport()
    {
        call Header()

        // Print brochure stuff

        call Footer()
    }
}