Digital Garden
Computer Science
Design Patterns
Factory

Factory

Factory patterns are so called creational patterns meaning their intent is to abstract/hide how objects are created. Which in return allows the client to be in independent of how its objects are created.

Factory method

The factory method pattern delegates the instantiation of objects to a method in either subclasses or a static method. The disadvantage of having the factory method as a static method is it can not be subclassed to change the behavior however you don't need to create an object to make use of the method.

Structure

Example

An example of the factory method pattern for a pizza store.

Abstract factory

The factory method pattern delegates the instantiation of object familys to a another object.

Structure

Structure of the abstract factory pattern.

A big question here is where is the concrete Factory so they can all have acces to it. Often this is done in it's own class

public class CurrentFactory {
 private CurrentFactory() { }; // prevents instantiation
 private static Factory fac = null;
 public static Factory getFactory() { return fac; }
 public static void setFactory(Factory f) {
  if (f == null) throw new NullPointerException();
  fac = f;
 }
}

Example

An example of the abstract factory pattern for a pizza store.