tiomno
tiomno

Reputation: 2228

Inheritance vs Composition in an specific context

I'm having second thoughts about using vertical inheritance or composition for a simple OOP implementation. I've read a lot, but I'm still bad at making up my mind. ^^

I need to produce four different reports: Quote, Invoice, Catalogue and Brochure: - Every report has the same Header and Footer. - Quote and Invoice contain the same formatted table with different data. - Brochure and Catalogue have different structures to the rest of the reports and no tables.

I'd like help on coming up with the OOP design here; I'll present my two ideas (pseudo code), but any feedback will be greatly appreciated. :)

Following inheritance

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

Following Composition for the table feature

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

Thanks a lot! :)

Upvotes: 0

Views: 59

Answers (1)

Ray Tayek
Ray Tayek

Reputation: 10003

This may be a religious question. Either way should work, and it will be easy to refactor from one to another. The standard logic says to favor composition over inheritance, but go with what feels right.

Upvotes: 1

Related Questions