0

This question may be too vague or broad, but i figured i'd give it a shot. I've inherited a large .NET project and have run into some things i've not seen before. The most pressing question i have, is what would be the difference between these two declarations? Both of them work, and both types are used in existing code, but i'm wondering if one should be used over the other for performance or security reasons.

var mgr = ManagerFactory.GetInstance<CustomerNotificationManager>();

vs.

CustomerNotificationManager cNotificationMgr = new CustomerNotificationManager();

Both result in an instance of the CustomerNotificationManager class that can be used for any methods within.

Let me know if you need any more info to (hopefully) answer my question. Also, if this question is 'answerable', feel free to suggest a better title.

public class ManagerFactory
{
    private static bool singleton = false;

    private static Dictionary<string, ManagerBase> instanceHolder = new Dictionary<string, ManagerBase>();

    public static bool Singleton
    {
        get { return ManagerFactory.singleton; }
        set { ManagerFactory.singleton = value; }
    }

    public static T GetInstance<T>() where T : ManagerBase, new()
    {
        if (singleton)
        {

            return getSingletonInstance<T>();

        }
        else
        {
            return new T();
        }
    }

    private static T getSingletonInstance<T>() where T : ManagerBase, new()
    {
        lock (instanceHolder)
        {
            Type genericType = typeof(T);
            if (instanceHolder.ContainsKey(genericType.ToString()))
                return instanceHolder[genericType.ToString()] as T;
            else
            {
                var instance = new T();
                instanceHolder.Add(genericType.ToString(), instance);
                return instance;
            }
        }
    }

}
TheValyreanGroup
  • 3,554
  • 2
  • 12
  • 30
  • If you check code of `ManagerFactory.GetInstance` it might not be only doing `new CustomerNotificationManager()`. Also if that factory is there it is there with a reason. – Chetan Jun 27 '17 at 00:59
  • @ChetanRanpariya I added the `ManagerFactory` class. It seems to be returning a singleton, and David's answer has helped a bit, but i'm still confused. – TheValyreanGroup Jun 27 '17 at 01:05
  • What is the confusion? If you understand the singleton then it should be clear that it not about how object is created but its about how to allow only one instance of class to be created during entire application life cycle. – Chetan Jun 27 '17 at 01:07
  • @ChetanRanpariya What is the reason for using one over the other and what would explain both ways being used in separate areas of the solution. – TheValyreanGroup Jun 27 '17 at 01:07
  • In my opinion, this question is not about `Factory vs instance constructors` but it's about understanding singleton vs not-singleton object creation. – Chetan Jun 27 '17 at 01:26
  • @ChetanRanpariya OP can easily edit the question to clarify that...and comment/vote to re-open. So far title and most of the post seem to ask about factory vs. new. (Alternative fate of the post is to get closed as opinion based which is likely even less useful). – Alexei Levenkov Jun 27 '17 at 02:43

1 Answers1

1

This is called the Factory Pattern, and it's intended to decouple the calling code from both the runtime type of the instance returned, and the details of object creation.

So GetInstance might return SomeSpecialCustomerNotificationManager, or MockCustomerNotificationManager, etc.

And it might perform some configuration on the instance before returning it.

Or it might return a singleton instance or an object from an object pool.

And all of those things could be changed later, or work differently during Unit Testing, etc, without requiring any changes in the code that uses the CustomerNotificationManager.

Community
  • 1
  • 1
David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67
  • I added the 'ManagerFactory' class to the post. It is returning a singleton, but i'm still confused as to what the benefit to one or the other is and why it would be used both ways in separate parts of the solution. – TheValyreanGroup Jun 27 '17 at 01:06
  • And now looking closer at the class, i see it will only return the singleton if that bool set `true`. So if it is not, then there is no difference between the two, correct? – TheValyreanGroup Jun 27 '17 at 01:11