开发者

Designing different Factory classes (and what to use as argument to the factories!)

Let's say we have the following piece of code:

public class Event { }

public class SportEvent1 : Event { }
public class SportEvent2 : Event { }

public class MedicalEvent1 : Event { }
public class MedicalEvent2 : Event { }

public interface IEventFactory
{
    bool AcceptsInputString(string inputString);
    Event CreateEvent(string inputString);
}

public class EventFactory
{
    private List<IEventFactory> factories = new List<IEventFactory>();

    public void AddFactory(IEventFactory factory)
    {
        factories.Add(factory);
    }

    //I don't see a point in defining a RemoveFactory() so I won't.

    public Event CreateEvent(string inputString)
    {
        try
        {
            //iterate through all factories. If one and only one of them accepts
            //the string, generate the event. Otherwise, throw an exception.
            return factories.Single(factory => factory.AcceptsInputString(inputString)).CreateEvent(inputString);
        }
        catch (InvalidOperationException e)
        {
            throw new InvalidOperationException("Either there was no valid factory avaliable or there was more than one for the specified kind of Event.", e);
        }
    }
}

public class SportEvent1Factory : IEventFactory
{
    public bool AcceptsInputString(string inputString)
    {
        return inputString.StartsWith("SportEvent1");
    }

    public Event CreateEvent(string inputString)
    {
        return new SportEvent1();
    }
}

public class MedicalEvent1Factory : IEventFactory
{
    public bool AcceptsInputString(string inputString)
    {
        return inputString.StartsWith("MedicalEvent1");
    }

    public Event CreateEvent(string inputString)
    {
        return new MedicalEvent1();
    }
}

And here is the code that runs it:

    static void Main(string[] args)
    {
        EventFactory medicalEventFactory = new EventFactory();
        medicalEventFactory.AddFactory(new MedicalEvent1Factory());
        medicalEventFactory.AddFactory(new MedicalEvent2Factory());

        EventFactory sportsEventFactory = new EventFactory();
        sportsEventFactory.AddFactory(new SportEvent1Factory());
        sportsEventFactory.AddFactory(new SportEvent2Factory());
    }

I have a couple of questions:

  1. Instead of having to add factories here in the main method of my application, should I try to redesign my EventFactory class so it is an abstract factory? It'd be better if I had a way of not having to manually add EventFactories every time I want to use them. So I could just instantiate MedicalFactory and SportsFactory. Should I make a Factory of factories? Maybe that'd be over-engineering?
  2. As you have probably noticed, I am using a inputString string as argument to feed the factories. I have an application that lets the user create his own events but also to load/save them from text files. Later, I might want to add other kinds of files, XML, sql connections, whatever. The only way I can think of that would allow me to make this work is having an internal format (I choose a string, as it's easy to understand). How would you make this? I assume this is a recurrent situation, probably most of you know of any other more intelligent approach to this. I am then only looping in the EventFactory for all the factories in its list to check if any of them accepts the input string. If one does, t开发者_StackOverflow社区hen it asks it to generate the Event.

If you find there is something wrong or awkward with the method I'm using to make this happen, I'd be happy to hear about different implementations. Thanks!

PS: Although I don't show it in here, all the different kind of events have different properties, so I have to generate them with different arguments (SportEvent1 might have SportName and Duration properties, that have to be put in the inputString as argument).


I am not sure about the input string question but for the first question you can likely use "convention over configuration"; a combination of reflection, the IEventFActory type and the naming you already have in place, Name.EndsWith("EventFactory") should allow you to instantiate the factories and get them into their Lists with code.

HTH ,
Berryl

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜