I am a noob in design principles and design patterns, this might seem like a very silly question.
Some classes in my code have the following structure (image linked):
As you can see, I have an interface implemented by 3 classes, and each class overrides the interface method execute(string, long, long)
.
Now, I need an extra function parameter for only the 3rd class, UpdateInDbOperation. I have shown my requirement in the linked diagram below (see the red marked portion in the diagram). For UpdateInDbOperation class, a fourth parameter of string type will be needed.
The problems:
- If I add a fourth parameter to my interface, the first two implementing classes will break.
- I am looking for a way to solve this issue without violating the open - closed principle.
Why I am implementing from IOperation interface:
- For simplicity I have NOT shown other methods of the
IOperation
interface.IOperation
contains some another method which is implemented by all 3 classes. No change is needed in that method. - In one region of my codebase, chain of responsibility design pattern is used to pass an object through all the classes implementing
IOperation
. - For these reasons, I cannot stop using
IOperation
. Simply put, up till now my design was working smoothly, and I thought it to be very extensible. But because of the extra parameter inUpdateInDbOperation
, I am confused over the whole design now.
What I have already tried / researched:
- The problem can be solved by using parameters with default values, but I want to avoid them. In the future similar situations may arise again. If I keep on increasing parameters with default values, then eventually the method callers will forget the actual number of parameters. Default value parameters will also add totally unused code to the first two classes.
- The problem can be solved by using C# params keyword (https://www.geeksforgeeks.org/c-sharp-params/), but this will make my interface extremely flexible. Personally I think an interface should provide a somewhat specific blueprint, but if I use C# params then virtually any class can implement the interface.
- I have tried to fit this issue into the Command design pattern. But most examples of Command I have seen use methods/functions without parameters.
- I have gone through one implementation of Command pattern in the following link (https://www.geeksforgeeks.org/command-pattern/?ref=lbp). This uses another secondary interface to isolate the method parameters. But I am not sure whether this can be used in my case.
Web links I have gone through: