4

I often use the class-factory pattern whereby a class has a private constructor and a static method to create the class. This allows for the situation where the class cannot be constructed for some reason, and a null is returned - very handy.

I would like to be able to extend this to a factory method which creates a particular class from a hierarchy of derived classes depending on conditions. However I can't see a way of then hiding the constructors of the derived classes to force the use of the factory method. If the factory method is in the base class it no longer has access to the private constructors of derived classes. Putting a factory method in every derived class doesn't work as the required type must then be known beforehand. Nested classes might be a way if a class had access to the private members of a nested class, but sadly it seems that the nested classes have access to the private members of the enclosing class, but not the other way round.

Does anyone know of a way of doing this?

Dave
  • 3,429
  • 2
  • 26
  • 29

4 Answers4

4

There are several possibilities, two of which are:

  1. Put all those classes in one project and make the constructors internal. Other projects won't be able to call those constructors but the code inside that project can.
  2. Make the constructors of those classes protected (instead of private) and create a private derived class in the class containing the factory method. Create an instance of that private class and return it.

Example for the second option:

public static class AnimalFactory
{
    public static Animal Create(int parameter)
    {
        switch(parameter)
        {
            case 0:
                return new DogProxy();
            case 1:
                return new CatProxy();
            default:
                throw new ArgumentOutOfRangeException("parameter");
        }
    }

    private class DogProxy : Dog { }

    private class CatProxy : Cat { }
}

public abstract class Animal { }

public class Dog : Animal
{
    protected Dog() { }
}

public class Cat : Animal
{
    protected Cat() { }
}
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Daniel and Mathew. Really clever. It works and fulfills all the requirements. I like it. I just don't know whose answer to tick as accepted :-) – Dave Feb 20 '13 at 12:35
  • @Dave: You have to decide that for yourself. Usually you accept the answer that helped you the most. If there are multiple answers that helped you equaly you either accept none or the one that you like more or has been posted first. – Daniel Hilgarth Feb 20 '13 at 12:37
1

Here's the sample code I was working on when Daniel posted his answer. It looks like it's doing what he suggested:

public static class BaseFactory
{
    public static Base Create(bool condition)
    {
        if (condition)
        {
            return Derived1.Create(1, "TEST");
        }
        else
        {
            return Derived2.Create(1, DateTime.Now);
        }
    }
}

public class Base
{
    protected Base(int value)
    {
    }

    protected static Base Create(int value)
    {
        return new Base(value);
    }
}

public sealed class Derived1: Base
{
    private Derived1(int value, string text): base(value)
    {
    }

    internal static Derived1 Create(int value, string text)
    {
        return new Derived1(value, text);
    }
}

public sealed class Derived2: Base
{
    private Derived2(int value, DateTime time): base(value)
    {
    }

    internal static Derived2 Create(int value, DateTime time)
    {
        return new Derived2(value, time);
    }
}

[EDIT] And for Daniel's second suggestion:

public static class BaseFactory
{
    public static Base Create(bool condition)
    {
        if (condition)
        {
            return new Derived1Creator(1, "TEST");
        }
        else
        {
            return new Derived2Creator(1, DateTime.Now);
        }
    }

    private sealed class Derived1Creator: Derived1
    {
        public Derived1Creator(int value, string text): base(value, text)
        {
        }
    }

    private sealed class Derived2Creator: Derived2
    {
        public Derived2Creator(int value, DateTime time): base(value, time)
        {
        }
    }
}

public class Base
{
    protected Base(int value)
    {
    }

    protected static Base Create(int value)
    {
        return new Base(value);
    }
}

public class Derived1: Base
{
    protected Derived1(int value, string text): base(value)
    {
    }

    protected static Derived1 Create(int value, string text)
    {
        return new Derived1(value, text);
    }
}

public class Derived2: Base
{
    protected Derived2(int value, DateTime time): base(value)
    {
    }

    protected static Derived2 Create(int value, DateTime time)
    {
        return new Derived2(value, time);
    }
}

Note that this second approach means that the classes can't be sealed, unfortunately.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Daniel and Mathew. Really clever. It works and fulfills all the requirements. I like it. I just don't know whose answer to tick as accepted :-) – Dave Feb 20 '13 at 12:36
  • Matthew, the first code is not what I meant with my first option. As I said, make the *constructor* `internal`. Your detour with the additional factory methods doesn't add any benefit. – Daniel Hilgarth Feb 20 '13 at 12:40
  • Aye fair enough. It was just intended to demonstrate the use with a factory method - although the factory method is so simple in this case that it doesn't hide anything. In the general case, it could do. – Matthew Watson Feb 20 '13 at 12:58
  • @Dave: Well Daniel was first to post the code, so if other factors are the same you should tick his. :) – Matthew Watson Feb 20 '13 at 12:58
0

You can intercept the derived type creation in the base class contructor and check that the caller is your factory using StackFrames:

 protected Class1() //base class ctor
    {
        StackFrame[] stackFrames = new StackTrace().GetFrames(); 
        foreach (var frame in stackFrames)
        {
            //check caller and throw an exception if not satisfied
        }
    }
Stefano Altieri
  • 4,550
  • 1
  • 24
  • 41
-1

Rather than using methods inside the class itself as a factory implement the Factory pattern by means of a static class ("the factory") that returns the correct instance based on the logic you write.

dutzu
  • 3,883
  • 13
  • 19
  • That doesn't solve the problem, which is: "Prevent users from creating instances via `new`". But I agree that the construction should be left to a third class. The base class shouldn't know about the classes derived from it. – Daniel Hilgarth Feb 20 '13 at 10:23
  • @DanielHilgarth You are right, for that he should separate the types in another project as internal and expose them through interfaces. – dutzu Feb 20 '13 at 10:28
  • Please re-read the question. I quote: "However I can't see a way of then hiding the constructors of the derived classes to force the use of the factory method". – Daniel Hilgarth Feb 20 '13 at 10:29