Let's say we have a code portion like this:
IProduct product = ProductCreator.CreateProduct(); //Factory method we have here
SellThisProduct(product);
//...
private void SellThisProduct(IProduct product)
{
//.. Do something here
}
//...
internal class Soda : IProduct
{}
internal class Book : IProduct
{}
How can I infer which product is actually passed into SellThisProduct() method in the method?
I think if I say GetType() or something it will probably return the IProduct type.
Object.GetType
returns the exact runtime type of the instance. That is what you should use.Although, generally speaking, you shouldn't care at all what the runtime type of an interface is - if you're writing code to determine that, it probably reflects an error in your design somewhere.
In fact, the interface name
IProduct
is already a bit of a code smell. It's not wrong, per se, but interfaces are meant to define the actions available on a particular object, i.e. what it does. The nameIProduct
seems to be describing what it is, not what it does, which is more appropriate for an abstract base class. This isn't a "rule", mind you, but it's a good guideline to follow.When writing methods/classes that depend on an abstract type (base class or interface), if you find that you're depending on the more derived types or specific implementations, it means that either your abstract type is anemic (does not have enough functionality to be used effectively), or your dependency has too much coupling (depends on implementation details instead of abstractions).
Consider expanding the
Product
/IProduct
to do more, or make your dependency actually work on specific product types through method overloading.While GetType() will return the actual type, you should use the
is
operator.Typically, you shouldn't need to do this. Normally, what you do is simply defer the behavior to the child class and invoke it via the interface. For example, if you have different requirements for Soda vs. Book (say, Soda requires collecting tax, whereas Book doesn't), then you'd create a Sell method on the interface and then in your SellThisProduct() method, you'd simply call the Sell() method on the object.
.....
typeof(product) will return IProduct.
product.GetType() will actually return the derived type of the object since it's a member function.
GetType
gets you the exact runtime type of an object. From the documentation:You can also use
is
to determine if an object is an instance of a specific type:Why do you need the exact runtime type, though? The entire point of an interface is that you should be hiding the implementation details behind the common interface. If you need to take an action based on the type, that's a big hint that you're violating the encapsulation it provides.
One alternative is to use polymorphism:
Since these three types aren't related at all, inheritance from a common type doesn't make sense. But to represent that they share the same capability of making noise, we can use the IVocalizer interface, and then ask each one to make a noise. This is a much cleaner approach: now you don't need to care what type the object is when you want to ask it to make a noise: