views:

247

answers:

5

I have encountered various classes that don't allow creation of their instance directly. Rather we have to create their instance from some other class's static method or it own static method. For example:

B b = A.getB();

or

B b = B.getInstance();

What reason is behind that?

Why don't they allow creating instance directly, as in:

B b = new B();
+4  A: 

Those are an example of the Singleton Pattern. They usually have private constructor method, but in the example I linked they used protected to prevent instantiation.

Abstract classes would not be able to be instantiated either.

Sean A.O. Harney
Or FactoryPattern.
Paul Tomblin
+1 for the link
Yatendra Goel
+5  A: 

Some classes want to control the way in which they are instantiated, and so protect their constructors from public use. Use static factory methods like getInstance allows them to keep that control within their own code.

There are a million reasons for wanting to do this.

edit: To address your comment, this cannot be done inside the constructor because the new operator will always create a new instance (unless an exception is thrown). By the time the constructor is invoked, it's too late for the code in the constructor to control whether or not an object is instantiated.

skaffman
+1 Could you please tell me why we can't write that controlling code in the constructor itself.
Yatendra Goel
See edited answer.
skaffman
+1  A: 

When objects have a complex creation logic, the designer delegates object creation to specific objects This is in accord to the single responsibility principle: the goal is to allow the object to ignore the structures and procedures required for its creation.

Without using a Factory, the creation of complex objects would require long contructors with too many parameters.

In the getInstance() case, the designer wanted to have a single instance of an object accessible throughout his application: it's the Singleton pattern and it's the equivalent of a global variable.

Silvio Donnini
+1  A: 

Both are implementations of creational patterns i.e. patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

  • The first example looks like the Factory method pattern: Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses [Gof].

  • The second example is the Singleton pattern: A factory which can only create a single instance of a class.

They don't allow to create instance directly with a new because they are precisely aimed at controlling how creation is done to solve a particular problem: restricting instantiation of a class to one object for the singleton, letting subclasses decide which class to instantiate for the factory.

Pascal Thivent
A: 

Some Use cases:

  1. With the Factory pattern only the interface of the Object will be visible, the implementation itself can be choosen and changed by the Factory.
  2. The createInstance Method does some additional work behind the scenes which can not be done within the ctor
  3. The class may use a Singleton for this object and always returns the same instance
josefx