Classical implementation of Abstract Factory/Factory method is based on polymorphism. But on the lowest level we have to have some kind of factory which is not based on polymorphism and breaks open-closed principle:
public Device Create(string deviceName)
{
switch (deviceName)
{
case "Device1": return new Device1();
case "Device2": return new Device2()'
default: throw new NotSupportedDeviceException(deviceName);
}
}
Is there any special name for this kind of factories?
From what I can see, you’ve posted a perfectly valid example of the Factory Method pattern:
It’s true, the internal implementation is a little heavy-handed (switch-case). However, that doesn’t make it any less of a true Factory Method pattern.
I don’t really see where your example “isn’t based on polymorphism” nor where it “breaks the open/closed principle”. If I’ve missed the point entirely, feel free to update your post, to help us zero in on your question.
Now if the Factory method was taking the passed deviceName, and using it to find an exact match of the concrete class to instantiate (using reflection), that would absolutely break the Factory Method pattern, because the caller would have to have intimate knowledge of the different concrete classes. But as currently written, the passed deviceName is just a piece of data used in the decision-making process – it doesn’t break the encapsulation of the factory pattern.