Suppose I have a number of methods with different signatures. Based on some external code all this methods might be called. You might think of them as some event handlers.
Now I should have two implementations as so:
- Each implementation really handles only part of all possible events.
- Each implementation simply does nothing for all events it do not want to / can not handle.
I could of course declare an interface for all possible handlers but then I will have to create empty methods (handlers) in each implementation. Even for those events I do not want to / can not process.
I am thinking about doing something like the following:
abstract class Base
{
public virtual void First(int i, double d) { /* no implementation */ }
public virtual void Second(double d) { /* no implementation */ }
public virtual void Third(string s, int i) { /* no implementation */ }
}
class Child : Base
{
public override void First(int i, double d) { /* implementation */ }
public override void Second(double d) { /* implementation */ }
}
class AnotherChild : Base
{
public override void Second(double d) { /* implementation */ }
public override void Third(string s, int i) { /* implementation */ }
}
This approach forces me to create empty implementations for all possible handlers in the base abstract class.
Could you recommend something better? An approach that doesn’t require to produce large number of empty methods?
I am using C# 2.0 and can’t use newer version of the language for this task.
I agree with @usr – I don’t see a problem with the empty functions. If you want to call a function, then it must exist. If it should do nothing in some cases, then that function should be empty. A base class with empty functions, versus an interface requiring the implementation of the same empty function over and over, seems like a very good idea.
If you are looking for an alternative, you could consider the Chain of Responsibility design pattern. Rather than calling a specific function, you could call a general function and then parameterize the desired behavior. You could then chain objects together (different chains in different situations) and give them all a chance to handle the behavior. If none of them handle it, then nothing happens.
This would work very well in some scenarios, but it’s more complicated to implement then the very simple and elegant base class approach. Be careful not to over-engineer.
EXAMPLE
Here’s an example of implementing a chain of command, based on the example you gave in your question:
(Please note that this example does not follow every programming best practice – I would not recommend implementing EXACTLY this code. For example, the action parameter would probably be better as an enum than a string, and returning some kind of CommandResult rather than a boolean. Use it for inspiration only.)