简体   繁体   中英

why use getInstance

A lot of the publicly available Java APIs seem to use getInstance in order to generate and return an object. I'm curious about why this is so -- why not just use default/parameterized constructors instead?

Is there an associated design pattern?

I suggest to read "Effective Java" by Joshua Bloch, Item 1 "Consider static factory methods instead of constructors". He led the design and implementation of numerous Java platform features, he knows why.

It is associated with Singleton Pattern .

Many times there are also Factory Methods which help you in creating objects.

For example: Boolean.parseBoolean("true");

Advantages of Factory Methods are they are more verbose and easy to grasp than a series of constructors.

Another example (apart from the singleton or multi-ton pattern) is if you need to do something in the constructor that is generally not recommended, such as registering a listener or starting a thread:

class Whatever {

    private Whatever() {} //private constructor
    public Whatever getInstance() {
        Whatever w = new Whatever();
        startSomeThread();
        return w;
    }
}

Yes....It is associated with both Factory and Singleton pattern. Factory pattern is used to decouple the object creation logic from the business logic and Singleton pattern is used to maintain only a single instance of an object through out the application.

To save you the trouble of looking up concrete reasons...

This is a case of the Factory Method pattern. The most common reasons to use it are:

  1. If construction is too complex to handle in a constructor
  2. If construction requires actions that are not permitted or desired in a constructor
  3. To decouple the type of the product ("interface") from the actual implementation ("class").
  4. For performance or architechtural reasons, such as to perform partial specialization at construction time
  5. For clarity/readability

There is some overlap between these reasons. In fact, often all four apply. Partial specialization (4) pretty much requires decoupling of type and implementation (3).

"why not just use default/parameterized constructors instead" because at the point you invoke the constructor it is already too late to ensure the Singleton Pattern constraint to have only one instance. The whole point is to have controlled access to a single instance and avoid multiple instances. The only way to do that is to use access modifiers to prevent the constructor from being accessible.

As it has been said, it's a matter of some pattern designs "requirements" to provide an easy/secure solution.

But you should avoid the using of "getInstance" (java.lang.Class.getInstance()' and 'java.lang.Class.NewInstance()') as far as possible in terms of Dynamic Instantiation , because Dynamic instantiation is slower than a regular Class invocation or Method call.

Use it only when it is necessary!

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