I have two very similar classes that do essentially the same thing. The only difference is in a callback handler provided to an instance of each class. The callback handlers are different and they are accepted with different parameters. I would like to generalize most of the code from these classes into a base class. Any ideas on how to generalize the delegate code intelligently? I'm on .NET 2.0
Note: I read this very useful blog on inheritance with delegates and articles on covariance and contravariance with delegates, but I still don't see how that knowledge can be applied here.
public class A
{
public delegate void AHandler(string param1, string param2);
public void AcceptHandler(string param3, AHandler handler);
public void InvokeHandler(string forParam1, string forParam2);
// the rest is same
}
public class B
{
public delegate void BHandler(int param1);
public void AcceptHandler(int param2, int param3, int param4, BHandler handler);
public void InvokeHandler(int forParam1);
// the rest is same
}
EDIT: "the rest" of the code is exact same, except calls to the delegate methods that have different signatures. Something like this:
public void StartListening()
{
Timer timer = new Timer(CheckForChanges, null, 0, 1000);
}
private void CheckForChanges()
{
// pull changes, and pass different params to InvokeHandler()
}