I am looking for an optimal way of implementing the code common between subset of interface implemtations. To describe it concretely assume we have an interface P that is implemented by A, B and C. Again, assume A and B have certain common functionality (common code), B and C have certain common functionlity and similarly A and C have common functionality in the implementation of the interface. I understand that:
- We can make intermediate class for each pair, containing the common functionality and then derive the classes from the intermediate classes, e.g., intermediate class AB implementing common code between A and B and BC for common code between B and C and then derive B from AB and BC.
- Replicate the code for the classes in each pair
1 should theortically be the optimal solution, however, practially it can create a huge mess because actually I don't just have 3 classes implementing the interface but a large number of them. Moreover, the common functionalities are not just between pairs but large subsets of the classes and there are many such subsets. Hence, we will need to have a large number of intermediate classes to implement the common functionality between different subsets. This makes it hard/inefficient to implement through intermediate classes, as we require large number of these intermediate classes.
I believe 2 is also not the best solution, because it requires code replication, which will cause the problems of code maintanance.
Edit: I have tried to simplify the question to give a clear picture in response to the comments.