I have a conceptual problem. I'd like to use the design pattern factory, using an enum to know what type of object to create. But when it comes to inherit in another assembly, I'm stick with my enum ...
Let me explain a case :
I'm creating a Framework for multiple projects that manage fruits.
I create an interface IFruit
implemented by an abstract class Fruit
, from which will inherit Strawberry
, Apple
and Pear
.
I create an enum FruitTypes
which contains Strawberry
, Apple
and Pear
.
I create FruitFactory
with a virtual method :
GiveMeANewFruit(FruitTypes newFruit)
which returns an IFruit
.
Everything's ok.
I send my Framework all over the world and someone in our spanish plant needs to manage also bananas.
He'll create Banana
class that inherits Fruit
.
He'll create a SpanishFruitFactory
that will inherit from FruitFactory
and redefine virtual method GiveMeANewFruit
, here is the code that could be in that method :
Public Override IFruit GiveMeANewFruit(FruitTypes newFruit)
{
if (newFruit == FruitTypes.Banana)
return new Banana();
else
return base.GiveMeANewFruit(newFruit);
}
But hey, here's the problem. I know languages like C# don't permit to inherit enums. So, my guy cannot add the Banana
value to the FruitTypes
enum.
So, what would be the best pattern to replace the enum so I can give the outside world the capability to add new object types to my concept ?
Knowing that I DO NOT want to have anything outside source code (no XML file, nor database nor anything else providing the types list).
Do you have any advice ?