The Factory pattern provides a class called a factory that can create several types of objects sharing a common interface of parent class.

The pattern is proposed in two flavors in Java Architect:

  • Direct factory where a specific create method is provided for each kind of object to create. The returned object is a concrete type.

  • Generic factory where a unique create method is provided that takes an enum parameter to indicate which type of object has to be created. The returned object is the common interface or class type.

Applying the Factory pattern

The pattern can be applied either to an Interface or to a Class used as an inheritance root. The pattern will recursively scan the inheritance tree starting with the selected root to find all the concrete sub-classes that the factory will support.

  1. In the model, select the existing root interface or class where you want to apply the factory pattern.

  2. From the contextual menu (rightclick) execute the module Java Architect → image Patterns → Create factory command.

  3. The command will pop the following GUI

Factory Pattern Dialog - Create
  1. Change the Name field value if the proposed one does not suit you. This is the name of the class to be created for the factory.

  2. Choose the implementation variant Direct or Generic factory.

  3. Press the OK button to apply the pattern and create the factory class model.

Tip The figure drawn in the dialog shows what the model will look like.