Makla
Makla

Reputation: 10459

Correct approach when multiple inheritance is needed (TypeScript)

First of all mixins are not the best solution, since my interfaces are large and I want to avoid writing empty implementations.

What I am looking for is which approach (technique) is the best for multiple inheritance. My problems is diamond inheritance (yes I read about diamond problem).

//                   -----------------------
//                   |         Edit        |
//                   -----------------------
//                  /                  \
//                 /                    \
//                /                      \
//    -----------------------         -----------------------
//    |     DataSetEdit     |         |        OEdit        |
//    -----------------------         -----------------------
//                    \                    /
//                     \                  /
//                      \                /
//                    -----------------------
//                    |     ODataSetEdit    |
//                    -----------------------

The logic is simple.

In one project I use DataSetEdit and in another I always use ODataSetEdit. So in project where ODataSetEdit is used, I only need to copy DataSetEdit functionality.

I do not want to duplicate code in DataSetEdit and ODataSetEdit. I was thinking if I can solve this with:

ODataSetEdit should extend OEdit but be able to use functionality added in DataSetEdit.

I think that the class structure is logic, how can I avoid need for multiple inheritance in my design? I am sure this is a common problem not related to TypeScript.

Upvotes: 2

Views: 598

Answers (1)

basarat
basarat

Reputation: 276199

how can I avoid need for multiple inheritance in my design

Move the logic out of OEdit into functions / variables (feel free to use a namespace : https://basarat.gitbooks.io/typescript/content/docs/project/namespaces.html to collect these into something meaningful) and then share them between OEdit and ODataSetEdit.

More

Fundamentally you need to move the logic out into a place accessible by at least one direct child and the grand child. No two ways around it 🌹.

Upvotes: 3

Related Questions