Tom Wright
Tom Wright

Reputation: 11479

Is there an advantage to using a static method which returns a new instance via a private constructor?

A pattern I occasionally see is like this:

public class JustAnotherClass
{
    private JustAnotherClass()
    {
        // do something
    }

    static JustAnotherClass GetNewClass()
    {
        return new JustAnotherClass();
    }
}

Why would this ever give an advantage over just having a public constructor?

Upvotes: 2

Views: 446

Answers (6)

Nicola Musatti
Nicola Musatti

Reputation: 18228

As it is in your example there's no real advantage. You use a factory method when you want to control when and how instances of your class are created. Some examples:

  • You want to implement a Singleton, that is always return the same instance;
  • You want to implement a cache and ensure that new instances are created only when no existing instance is available;
  • You need to control when instances are created based on external information. For instance you might be mapping the file system and want to ensure that no two instances of your File class exist for the same pathname.

Upvotes: 0

CodesInChaos
CodesInChaos

Reputation: 108810

  1. It can return a derived class.
    Sometimes you have different internal implementations of a base class, and the consumer shouldn't know which one he got, since it's an implementation detail.
  2. It has a name.
    I often use it instead of overloading the constructor, so it becomes clearer what the meaning of this new instance is.

One example from a recent project of me: I have a class representing an asymmetric key-pair. The constructor is protected and there are two factory methods: FromPrivateKey(byte[]) and GenerateIdentity(). IMO this makes consuming code easier to read.

Upvotes: 0

Azodious
Azodious

Reputation: 13872

I don't see any advantage of having a static method just to create a new object. It is more or less equvalent to directly call constructor.

it makes code more scaleable which won't be possible with public constructor. Check Henk holterman's answer also.

Upvotes: 0

Jan
Jan

Reputation: 2168

Besides from being for flexible, you need this approach if you want to use parameters in your constructor (at least this behavior) and XML serialization at the same time.

Upvotes: 0

Rob Smyth
Rob Smyth

Reputation: 1858

Good question. The class you show is a factory (see factory pattern). So 'why use a factory' ... as I said a good question.

For me, I use factories when I need to create instances at run time (many times). Why? Because it makes my code some much easier to test using unit testing. This is one answer to you question and it is irrelevant if you do not unit test (and perhaps TDD) your code. No wrongs or rights here, just a fact.

To answer you question ask 'why use a factory'.

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273264

Why would this ever give an advantage over just having a public constructor?

It's a factory pattern. You have a single point where these instances are made.

The advantage would be that in a future extension you could add logic, like returning a derived class instance. Or to return null under certain conditions. A constructor cannot return null.

Upvotes: 4

Related Questions