I found the following implementation in a code base I am going through
Interface IOrder
{
GetSubmittedOrder();
CreateOrder();
GetExistingOrder();
}
Class Order : IOrder
{
BuildSpecialOrder();
AddSpecialOrderParameters();
IOrder.GetSubmittedOrder();
IOrder.CreateOrder();
IOrder.GetExistingOrder();
}
Now, when we want to access the last three methods from this Order object, we need to do the following declaration
IOrder objOrder = new Order();
What is the reason(advantages) for creating an implementation like this? Does this practice have a specific name?
Note: Please let me know if this is a better fit at programmers.
I would say that that’s the incorrect usage of explicit interface implementation. It basically means that the methods are not visible in the public class contract. That’s why you have to cast the object to the interface type first (you are using an implicit cast). You could also have done:
It’s normally used when the class method collides with an interface method or when a class implements two interfaces with the same method (but different purposes).
It should be used with care since it can indicate that your class have a too large responsibility (and should be divided into smaller classes).