Skip to content

Instantly share code, notes, and snippets.

@lawloretienne
Last active November 3, 2016 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lawloretienne/e8d336128d8625caaa9d22570ec3472a to your computer and use it in GitHub Desktop.
Save lawloretienne/e8d336128d8625caaa9d22570ec3472a to your computer and use it in GitHub Desktop.
  • Consider static factory methods instead of constructors
    • One advantage of static factory methods is that, unlike constructors, they have names.
    • A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
    • A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
    • A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.
    • The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
    • A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.
  • Consider a builder when faced with many constructor parameters
    • The telescoping constructor pattern works, but it is hard to write client code when there are many parameters, and harder still to read it.
    • JavaBeans Pattern - allows inconsistency, mandates mutability
    • Because construction is split across multiple calls, a JavaBean may be in an inconsistent state partway through its construction
    • The JavaBeans pattern precludes the possibility of making a class immutable and requires `added effort on the part of the programmer to ensure thread safety.
    • The Builder pattern simulates named optional parameters as found in Ada and Python
    • Class.newInstance breaks compile-time exception checking.
    • The Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters, especially if most of those parameters are optional.
  • Enforce the singleton property with a private constructor or an enum type.
    • Making a class a singleton can make it difficult to test its clients, as it’s impossible to substitute a mock implementation for a singleton unless it implements an interface that serves as its type.
    • A single-element enum type is the best way to implement a singleton.
  • Enforce noninstantiability with a private constructor.
    • Attempting to enforce noninstantiability by making a class abstract does not work.
    • A class can be made noninstantiable by including a private constructor.
  • Avoid creating unnecessary objects
    • Prefer primitives to boxed primitives, and watch out for unintentional autoboxing
  • Eliminate obsolete object references
    • Nulling out object references should be the exception rather than the norm.
    • Whenever a class manages its own memory, the programmer should be alert for memory leaks
    • Another common source of memory leaks is caches.
    • A third common source of memory leaks is listeners and other callbacks.
  • Avoid finalizers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment