Binding of method implementations to interface methods that they implement is done by method signature, i.e. the name and the parameter list. The class that implements an interface with a method Register
must have a method Register
with the same signature. Although C# lets you have a different Register
method as an explicit implementation, in situations like that a better approach would be to go for the Bridge Pattern, which lets you "connect" an interface to an implementation with non-matching method signatures:
interface IMyInterface {
void Register(string name);
}
class MyImplementation {
public void RegisterName(string name) {
// Wrong Register
}
public void RegisterName(string name) {
// Right Register
}
}
The bridge class "decouples" MyImplementation
from IMyInterface
, letting you change names of methods and properties independently:
class MyBridge : IMyInterface {
private readonly MyImplementation impl;
public MyBridge(MyImplementation impl) {
this.impl = impl;
}
public void Register(string name) {
impl.RegisterName();
}
}
When make changes to one of the sides of the bridge, you need to make the corresponding change in the bridge to be back in business.