1
public class A {
    private A(int param1, String param2) {}

    public static A createFromCursor(Cursor c) {
        // calculate param1 and param2 from cursor
        return new A(param1, param2);
    }
}

Is there a design pattern for this kind of construction code? If so, what's the purpose of this pattern? Why not just use:

// calculate param1 and param2 from cursor
new A(param1, param2);
Johnny
  • 6,239
  • 7
  • 29
  • 36

3 Answers3

3

So, to sum up.

That pattern is called Static Factory Method and it's described for example here: How to use "Static factory methods" instead of constructors?

In simplest form it looks like this:

 class A {

     public static A newA() {
        return new A();
     }
     private A(){}
 }

Your example is little bit more complex as it includes computing parameters for invoking constructor

public class A {
    private A(int param1, String param2) {}

    public static A createFromCursor(Cursor c) {
        // calculate param1 and param2 from cursor
        return new A(param1, param2);
    }
}

The purpose of using such way for creating new object may be the need to repeat those calculating every time before calling new A(params) directly. So it's just avoiding repeating yourself, and simplest way to achieve this is creating a method.

Furthermore using the same way, you can provide even more options to create new A. For example you can change the way your calculation works with:

    public static A createFromCursorDifferently(Cursor c) {
        // calculate param1 and param2 from cursor in different way
        return new A(param1, param2);
    }

Then you may pass the same parameter to this method, and result will be different (coz method name is varies).

And of course you can create your new A from any other parameter, using the same constructor as before:

    public static A createFromObject(Object o) {
        // calculate param1 and param2 from object
        return new A(param1, param2);
    }

So you have much more possibilities with Static Factory Methods than with simple usage of constructors only.

Community
  • 1
  • 1
dantuch
  • 9,123
  • 6
  • 45
  • 68
  • 1
    I think you missed two of the common uses of this pattern. One is that the static method can choose which class to instantiate -- it can do something like `if (conditionA) return new A(...) else return new B(...)`. The other is to allow a caller to instantiate a class whose constructor, for whatever reason, you don't want to expose publicly (for instance, because you want to limit who can create subclasses of it). `ByteBuffer` is a good example of this. – yshavit Jun 16 '12 at 19:32
  • @yshavit you're right, but I tried stick to given question, not to describe whole nature of this pattern, and factoring as general :) – dantuch Jun 16 '12 at 19:39
2

If your code compiled, it would represent a simple factory method (it returns a static what? an A of course). Some consider this a pattern, but others don't as it is very simplistic. There is a pattern called the Abstract Factory Pattern that uses this concept, but in a much more complex fashion.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
1

The purpose of the thing you posted should be, if corrected, the one of providing a method to return an instance of something that extends A without the necessity to know the runtime type a priori.

So yes, it sounds similar to a Factory method pattern although it's somewhat different from what you wrote. The factory class should be different from the product class.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • `The factory class should be different from the product class.` that's true, that's why the code provided as a sample is example of **Static Factory Method**, as pointed by @Tomasz Nurkiewicz. And that static method should be placed in the same class that it is factoring, just to replace explicit constructor. It is well described in "Effective Java" - one of the most recommended books for java programmers – dantuch Jun 16 '12 at 17:37