0

Android's VelocityTracker does not use a constructor, but rather if the object reference is null, you use variableName = VelocityTracker.obtain(). I have never seen something like this in Java before. Is this a standard design pattern, and why is it used?

Taylor Kline
  • 908
  • 3
  • 9
  • 30
  • Factory method, it returns an instance as opposed to using constructor – Brandon Ling Jun 15 '15 at 18:08
  • possible duplicate of [What are static factory methods in Java?](http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java) – David Jun 15 '15 at 18:17

1 Answers1

2

It's a factory method. (At least) in Android, obtain() is a conventional method name for classes that implement recycling under the hood. They usually come with a recycle() method that you run on an object when you no longer need it (another good example is Message). That is, they keep track of a pool of objects and, whenever there comes a need for a new object, you call obtain() which may return an already created instance instead of allocating memory for a new one.

SqueezyMo
  • 1,606
  • 2
  • 21
  • 34