Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 747691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:14:05+00:00 2026-05-14T14:14:05+00:00

Let’s say we have the following piece of code: public class Event { }

  • 0

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, then 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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:14:05+00:00Added an answer on May 14, 2026 at 2:14 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a class like this: public class Person { private String
Let's say I have the following within my source code, and I want to
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.