0

I'm doing a demo application and I need some guidance to find the best practice. So I planned to have 2 tables, with the following columns:

Table A:
    name
    quantity
    length
    color

Table B:
    name
    quantity
    length
    type
    ingretients

I want to build a model structure which extract the common fields into an abstract class. So I planned the following classes:

Class abstract X
    name
    quantity
    length

Table A extract X:
    color

Table B extract X:
    type
    ingretients

My first question is: Is this a good approach?

Also I planned to design a service which calculates the average lenght of an item (I din't want to use sql queries for this). I was thinking of one service for each class or do one generic service which calculates this value. What do u think, which is the better approach and why?

HowToTellAChild
  • 623
  • 1
  • 9
  • 21
  • While it's usually a good idea to try and reuse code and data, you also have to remember that inheritance is an "is-a" relationship. Is "B" also an "X"? If not then perhaps that's not really a good idea. I also recommend you make it a habit to use classes to encapsulate *behavior* instead of just containers of data. Unless both "A" and "B" share behavior, it's also not a good idea to have them share a common parent. IMO. – Some programmer dude Sep 02 '22 at 10:16
  • So "X" was created because "A" and "B" classes carried same fields with the the purpose so it seemed a good idea the collect them into a common parent. But in real life these are different things but similar set of attributes. Could you please share why you think a bad idea to share common parent? – HowToTellAChild Sep 02 '22 at 14:38

1 Answers1

0

Is this a good approach?

In my view, it is ok as you removed variable duplications. Read more here about member variable inheritance.

I was thinking of one service for each class or do one generic service which calculates this value. What do u think, which is the better approach and why?

If calculations are the same then generic version is better because you will have just one implementation and you will not have duplications of code.

StepUp
  • 36,391
  • 15
  • 88
  • 148