Item 1. Static Factory Methods

Consider static factory methods instead of constructors.

A class may provide clients with static factory methods instead of, or in addition to, constructors. These are used to replace constructors with carefully chosen names.  Can be similar to the Flyweight pattern.  The returned class can be non-public, and can vary from invocation to invocation.  This is not the same as the factory method pattern.

Constructors that differ only in the order of their parameter types must use static factory methods.

Supports a service provider framework, which contains three key required and one optional components:

  1. service interface, which providers implement
  2. provider registration API, which the system uses to register implementations
  3. service access API., which clients use to obtain an instance of a service.
  4. service provider interface, which providers implement to create instance of their service implementation.

Example TODO Markings to put into your code to suggest the usage of this item.

  • TODO: make instance controlled
  • TODO: make static factory method
  • TODO: refactor into a service provider interface.
  • TODO: service access API
  • TODO: service provider interface

Advantages

  • they have names, unlike constructors. this helps describe the object being returned. Easier to read.
  • they prevent the wrong constructor from being called by mistake
  • people reading the code will know what the code does without referring to the class documentation.
  • they are not required to create a new object each time they’re invoked.
  • supports immutable classes to use preconstructed instances or to cache instances as they’re constructed and dispense them repeatedly to avoid creating unnecessary duplicate objects.
  • can greatly improve performance if equivalent objects are requested often, especially if they are expensive to create.
  • allows instance-controlled classes: strict control over what instances exist at any time.
  • an object of any subtype of the class can be returned.
  • allows the return of objects without making them public – supports interface-based frameworks a very compact API.
  • reduces conceptual weight
  • reduces the need to read additional class documentation for the returned classes.
  • requires a client to refer to the returned object by its interface rather than its implementation class (item 52).
  • supports service provider frameworks (e.g. JDBC API) – a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations.
  • service provider framework supports the service provider framework pattern.
  • reduces the verbosity of creating parameterized type instances.

Disadvantages

  • classes without public or protected constructors cannot be subclassed.
  • not readily distinguishable from other static methods.  Can be reduced by drawing attention to static factory methods through the use of common names like “valueOf”, “of”, “getInstance”, “newInstance”,  “getType”, “newType” (where Type is an arbitrary type).

One thought on “Item 1. Static Factory Methods

  1. Pingback: Effective Java #1: Static Factory Methods | Richard's Mobile Blog

And now it's your turn ... comment here or on Twitter (@Androider)

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s