简体   繁体   中英

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?

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 .

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.

  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.

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'.

除了要灵活之外,如果要同时在构造函数中使用参数(至少是这种行为)和XML序列化,还需要这种方法。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM