A Factory pattern


The idea popped in to my head, so I wrote it down.  I’m not sure what it’s good for, but here it is:

 

    class C

    {

        private C() { }

        public static class Factory

        {

            public static C New()

            {

                return new C();

            }

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            C c = C.Factory.New();

        }

    }

 

Comments (7)

  1. more generic, less useful:

    class Factory

    {

    public static T Create<T>()

    {

    return new T();

    }

    public static T Create<T,V>(V modifier)

    {

    return new T(modifier);

    }

    public static T Create<T>(params object[] modifiers)

    {

    return new T(modifiers);

    }

    }

    class Program

    {

    public static void Main(string[] args)

    {

    int[] someArray = new int[] {3,5,7};

    List<int> someList = Factory.Create<List<int>,IEnumerable<int>>(someArray);

    }

    }

    untested doesn’t even begin to describe this – no idea if it’s even close to legal code 🙂

  2. Mark Blomsma says:

    The idea looks cool, I’ve toyed around with a similar implementation of the Factory, but the weak point is that the factory implementation is now quite strongly tied to the actual class begin returned.

    A new, future implementation will still need the original class for the factory.

  3. Luc Cluitmans says:

    This is closely related to a suggestion for a language improvement, listed as FDBK14659 in LadyBug ( http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=FDBK14659 ). Thanks for giving me an opportunity to actually demonstrate it for the purpose I intended it for 🙂

    The following code uses a concept of a ‘static interface’, which of course is completely illegal (hence the suggestion), in this case to let a class implement its own static factory method (no separate factory class required). But the same concept could be used to allow calling overloaded operators on template arguments.

    One more note before the actual code: in C# v1 there would be no way to actually use these ‘static interfaces’, because interfaces require an object instance to resolve the type they apply to. In v2 however, these can be used, namely through the type arguments of templates, and only through them.

    Here is the code, using the generic type A as its factory argument for this example:

    static interface SFactory<A, T> {

    T Create(A arg);

    }

    public class Item: SFactory<string, T> {

    … // constructor omitted for brevity

    static T Create(string arg) {

    return new T(arg);

    }

    }

    // example of using this class:

    public class MyCache<T>

    where T: SFactory<string,T> {

    private Dictionary<string,T> FStore = …;



    // Look up the item; if it doesn’t exist,

    // create it (using the key as argument).

    public T GetItem(string key) {

    if(!FStore.ContainsKey(key)) {

    FStore[key] = T:Create(key);

    }

    return FStore[key];

    }

    }

    class Program

    {

    public static void Main(string[] args) {

    MyCache<Item> cache = new MyCache<Item>();

    foreach(string arg in args) {

    DoSomethingWith(cache.GetItem(arg));

    }

    }

    }

  4. Sameer Murukdar says:

    WebRequest class implements a similar method Create, the difference being WebRequest is an abstract class.

    Another use is if you want to create only one instance of the class. In the New method if an instance of the class exists you return a reference to the instance. If not create a new instance as you are doing it now and return a reference.

  5. Jay Bazuzi jots down a great little idea for a factory pattern that popped into his head. He mentioned that he didnt know what it’s good for. I’ll agree with him… by itself, it really isnt good for anything, as…

  6. Travis says:

    I’ve done this with an abstract base class which defines the New() method. Every concrete class that is derived from the base returns an instance of its own specialized type.

    I used it as part of an interchangable grammar on a parser. You give the parser constructor a set of concrete factory objects. You can then call New() on the appropriate object when you need a new one.

    Also see Activator.CreateInstance(typeof(T));

  7. AndrewSeven says:

    I’ve been working on a Factory/Strategy/Provider thingy.

    A lot like the provider model, but no static class as I have layers that add methods and that doesn’t work with statics

    Won’t that VB stuff choke on the method called "New"?