GurdeepS
GurdeepS

Reputation: 67283

Differences between static class and instance class with private constructor

Although a static class has only one instance and can't be instantiated, a class with a private constructor can't be instantiated (as the constructor can't be seen), so every time you call this class, this is the same one instance?

Factory classes always follow the last convention (instance class with private constructor). Why is this?

Thanks

Upvotes: 4

Views: 2026

Answers (3)

Anthony Pegram
Anthony Pegram

Reputation: 126992

You can't* get an instance from outside the class, but you can from inside. A static method or an inner class can create and return an instance of the class with a private constructor. The static class cannot be instanced by anything.

class Foo
{
    private Foo()
    {
    }

    public class Bar
    {
        public Bar()
        {
        }

        public Foo GetFoo()
        {
            return new Foo();
        }
    }
}

..

Foo.Bar fooBar = new Foo.Bar();
Foo foo = fooBar.GetFoo();

Edit: *I use the term "can't" loosely. Brian Rasmussen pointed out in the comments to the OP that another method to obtain an instance is through a call through System.Runtime.Serialization.FormatterServices, and this is external to the class itself.

Foo foo = (Foo)System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(typeof(Foo));

Upvotes: 1

Andy White
Andy White

Reputation: 88475

Creating a class with private constructor is the common pattern for implementing a "Singleton" object.

The Singleton usually will instantiate an instance of itself, and only allow access to it through a static "Instance" property, which means there's only ever one instance of the class.

The advantage of using a Singleton over a purely static class is that you can utilize interfaces and different implementation classes within the singleton. Your "Singleton" might expose an interface for a set of methods, and you can choose which exact implementation class to instantiate under the covers. If you were using a purely static class, it would be hard to swap out a completely different implementation, without impacting other code.

The main downside of Singleton is that it's difficult to swap out the implementation class for testing, because it's controlled within the Singleton private methods, but there are ways to get around that.

Upvotes: 0

Jesse C. Slicer
Jesse C. Slicer

Reputation: 20157

There's nothing stopping the class with the private constructor from having a public static method which returns instances of the class:

public class NoPublicConstructor
{
    private NoPublicConstructor()
    {
    }

    public static NoPublicConstructor NewInstance()
    {
        return new NoPublicConstructor();
    }
}

As you can see, the static method does not return the same one instance.

edit: One of the reasons factory classes do this is to be able to separate responsibility in future versions: while your code always calls the factory creation method, the author may move all the "guts" out of that class into a different one and your code won't need to know the difference. Calling that class' (public) constructor ties it to an extent to the original class implementation.

Upvotes: 4

Related Questions