2

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.

  • Edit is basicaly some <input type="textbox"> with some additional method for validation, masks, input checking ...
  • OEdit extends edit with some additional methods for styling. Can get properties from server application (WebSocket), send events to server
  • DataSetEdit adds methods and properties for handling edit properties (text, color, caption, placeholder ...) - automatically handle changes when current record has changed. (DataSet is class and its data is Object[].)
  • ODataSetEdit extends OEdit functionality but need also DataSetEdit methods for handling changes on DataSet.

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:

  • some static methods/properties approach
  • decorators (maybe on constructor to extend class functionality)
  • some static method on DataSetEdit which would be called from ODataSetEdit to extend OEdit functionality with DataSetEdit
  • proxy methods and properties from ODataSetEdit to DataSetEdit
  • some sort of composition (how would design looks in this concrete example)

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.

1 Answer 1

3

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 🌹.

Sign up to request clarification or add additional context in comments.

2 Comments

Also, several layers of inheritance is often a big design smell and a maintainance hazard, so moving to smaller abstractions (functions) is a big win.
Agree, also buy and read some books about that, but examples in books are to basic for me to transact it to my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.