Below are three implementation of the interface. The only difference between implementation 1 and implementation 2 is the namespace containing the ItemWrapper. Can this be refactored using generalization or something else perhaps?
interface IItemProvider {
object GetItem(Item item);
}
//1
class SomeNamespaceProvider : IItemProvider {
object GetItem(Item item) {
return SomeNamespace.ItemWrapper.GetItem(item);
}
}
//2
class OtherNamespaceProvider : IItemProvider {
object GetItem(Item item) {
return OtherNamespace.ItemWrapper.GetItem(item);
}
}
//3
class TotallyDifferentProvider : IItemProvider {
object GetItem(Item item) {
return DifferentItemRetriever(item);
}
}
From your description I understand that you have static methods in classes
SomeNamespace.ItemWrapperandOtherNamespace.ItemWrapperand they don’t implementIItemProviderinterface. You want to turn those static methods to instance methods and you don’t know how to do it.If classes
SomeNamespace.ItemWrapperandOtherNamespace.ItemWrapperaren’t static you could implementIItemProviderexplicitly:If they have to stay static or for any reason you don’t have access to the code I would recommend some functional approach:
Then you sould be able to use `GenericItemProvider’ like that:
In both solution there is no need to create more classes for evey namespace like
SomeNamespaceProviderclass you had in the firs place.