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 623891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:08:31+00:00 2026-05-13T19:08:31+00:00

Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits

  • 0

Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits back a new instance of a concrete implementation of some interface. What kind of design is this? Is there a better way to do what I want?

Here’s some code to illustrate the point:

using System;
using System.Collections.Generic;

// static factory class
public static class ArticleFactory 
{
    // given an SKU, store the Type object for an IArticle object
    private static Dictionary<string, Type> articleRegistry = new Dictionary<string, Type>();

    // allow public registration of SKU-to-Type object relationships
    public static bool Register(string sku, Type typeInfo)
    {
        if(!articleRegistry.ContainsKey(sku)) 
        {
            articleRegistry.Add(sku, typeInfo);
            return true;
        }
        return false;
    }

    // given a SKU, give me an instance of the related IArticle object
    public static IArticle NewArticle(string sku)
    {
        if(articleRegistry.ContainsKey(sku))
        {
            // use reflection to invoke the default constructor
            return articleRegistry[sku].GetConstructor(Types.EmptyTypes).Invoke(null) as IArticle;
        }
        return null;
    }
}

// example concrete-implementation of an IArticle
public class Jeans : IArticle 
{
    public decimal GetPrice() {  return SomeDecimal(); }
}

// WHERE DO I CALL THIS LINE? 
ArticleFactory.Register("0929-291", typeof(Jeans)); 

// Later on, if another group needs to write the class for Snowboards, 
// how can they self-register their class, without changing any "Main()"
// or "Page_Init()" function?
  • 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-13T19:08:31+00:00Added an answer on May 13, 2026 at 7:08 pm

    Looks like you’ve already identified the pattern. It’s the Factory Method Pattern. Or rather, a somewhat half-baked implementation of one. A slightly better approach would be to first make it an interface:

    public interface IArticleFactory
    {
        IArticle CreateArticle(string sku);
    }
    

    Then implement the factory without any Reflection at all:

    public class MyArticleFactory
    {
        private Dictionary<string, Func<IArticle>> instantiators =
            new Dictionary<string, Func<Iarticle>>();
    
        public MyArticleFactory()
        {
            Register("Jeans", () => new Jeans());
            Register("Shirt", () => new Shirt());
            // etc.
        }
    
        public IArticle CreateArticle(string sku)
        {
            Func<IArticle> instantiator;
            if (creators.TryGetValue(sku, out instantiator))
                return instantiator();
            throw new UnknownSkuException(sku);
        }
    
        protected void Register(string sku, Func<IArticle> instantiator)
        {
            creators.Add(sku, instantiator);
        }
    }
    

    A few important differences:

    • Registration isn’t public, nor should it be. Registration usually either resides in a configuration file somewhere or is private.

    • Does not require the IArticle concrete types to have a default parameterless constructor. This can easily register articles with parameterized constructors (as long as it knows what parameters to use).

    • Throws an exception on duplicate registrations. I don’t like the idea of simply returning false; if you try to register the same factory method twice, that ought to be considered a bug.

    • It’s not static. You can replace this factory with a different factory. You can unit-test it.

    Of course, an even better approach would just be to use any of the myriad of existing .NET Dependency Injection/Inversion of Control Frameworks, such as Ninject or AutoFac.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say that I have a singleton class (Downloader) responsible for downloading and persisting files.
so let's say you have a singleton pattern or whatever: class Smth{ public static
Say you have an enum singleton: public enum Elvis { INSTANCE; private int age;
Let's say I have a MySingleton class with a static constructor. I want that
Let's say I have a singleton class like this: class Settings include Singleton def
Lets say have this immutable record type: public class Record { public Record(int x,
Say I have the following generic class: public class Foo<T extends Bar> { //
Say I have a method that returns this. Vector[ (PkgLine, Tree) ]() I want
Say I have an input consisting of 30 numbers, and I want the 1st,
Say I have some foo :: Maybe Int and I want to bind it

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.