OO Design Heuristics

Interfaces - don't get coupled without them

  1. Clients should never "name" concrete classes.
  2. Every type should be defined using an interface.
  3. All method parameters and return values should be declared using interface types.
  4. The only time a class type is referenced is when an object is created.
Define an interface where heterogeneous classes are accessed as a single type. Approach the design of an interface from the client's perspective that wants to treat otherwise incompatible classes interchangeably. Consider where clients would need a single facility to operate across divergent classes and the name of that facility might be given an "-able" suffix (Cloneable, Serializable, Compressible, Drawable).

Factory methods - the final frontier of "insulation"

Constructors are coupled to their class. They have no choice but to initialize an object of their specific type. Factory methods are free to provide a "virtual contructor" capability. They can evaluate their arguments at run-time, optimize their decision across many dimensions of variability (choice of implementation, location, reuse or sharing of existing objects, persistence mechanism) and return the right object as an abstract type.

Some ideologues insist that all constructors should be private or protected. The only way to create instances of a class should be through factory methods. It's nobody's business but the class' whether a class manufactures a new object or recycles an old one.