4

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 ?

clcto
  • 9,530
  • 20
  • 42
Sierramike
  • 371
  • 4
  • 16
  • 3
    It is not java, and use MEF. – clcto Mar 27 '14 at 15:44
  • Here is an introduction to MEF: http://channel9.msdn.com/Blogs/kreekman/TechDays-2010-Managed-Extensibility-Framework-MEF-through-Silverlight – clcto Mar 27 '14 at 15:51
  • 2
    @clcto Simply saying "use MEF" with absolutely no indication of what you're referring to or how it may be relevant isn't exactly helpful. I'm really not sure what the Mount Everest Foundation has to do with this. – Chris Mar 27 '14 at 15:51
  • 1
    Have you looked at this: http://stackoverflow.com/a/757815/1693085 - or http://stackoverflow.com/a/4042826/1693085 - Could these maybe do it?? – John Bustos Mar 27 '14 at 15:52
  • @Sierramike I'd use the second link in John Bustos' comment – Pacane Mar 27 '14 at 17:23
  • @clcto : OMG, I know MEF, but it's somewhat overkill for what I want to achieve. Also, their file and directory structure imposed for MEF to work makes it non friendly, I want to keep it simple to use for other developers. Thanks anyway. – Sierramike Mar 27 '14 at 20:13
  • @John Bustos : I think your second link, as says Pacane, is the most usefull, I wish you had put that as an answer so I could mark it accepted ! Thanks ! – Sierramike Mar 27 '14 at 20:14
  • I just posted it as a solution, but wish the original poster had... His solution is written nicer than I ever would have managed :p Either way, glad it gave you what you're looking for!! – John Bustos Mar 27 '14 at 20:19
  • As I mentioned in the comments, it seems like the best solution to this was already answered very well in [this link](https://stackoverflow.com/a/4042826/1693085). Glad it worked for you!! – John Bustos Mar 27 '14 at 20:19

2 Answers2

0

I think the best replacement is static class. That's the way I'm doing it in php. Define static class with fields instead of enum, you can even define fields as string if you prefer.

Elvedin Hamzagic
  • 825
  • 1
  • 9
  • 22
0

Change the factory method to accept an int, and implement enums using classes such as these:-

public class FruitTypes
{
    public const int Strawberry = 1;
    public const int Apple = 2;
    public const int Pear = 3;
}

public class SpanishFruitTypes : FruitTypes
{
    public const int Banana = 4;
}

Example usage:-

factory.GiveMeANewFruit(FruitTypes.Pear)

factory.GiveMeANewFruit(SpanishFruitTypes.Banana)
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152