views:

78

answers:

1

You have a class, that perfectly fits to be an abstract, but this class cannot work normally without data supplied from derived class. It's not convenient to pass all data to constructor because not all of it may be needed, and many of them can be dynamic (result from child function).

What are the best practices to compose such structure? Or is it a bad design in the first place?

  1. Declare abstract functions, that child must implement?
  2. Declare normal functions, that are overriden in child class?
+1  A: 

If there is a reasonable default behaviour of the function that can be relied on in a majority of children, it can be implemented as a normal one. Otherwise (no default behaviour and/or most children have to override it), it's better to declare it as abstract. The side effect is that you never forget to define it in a child class because failure to do so will be immediately reported.

The other opportunity is the use of containers (for example, arrays) for the data that are specific to the child classes, but still are subject for general checks or other processing that can be done in the common ancestor. This way you define a variable that holds the data, but the data itself is filled in the children. The code in the parent class can iterate over this container and do some routine work on all the elements.

kwaxer
Good answer, thank you.
Deniss Kozlovs