One of the goals in object-oriented design is to delegate responsibility among different objects. This kind of distribution is good as it reflects two important features in object oriented methodology, encapsulation and delegation.

1. Problem

Often, we find ourselves in one of the following situations that needs a solution.

  • An application (or framework) at runtime, is incapable of anticipating the class of object/instance. The application may know that it has to instantiate classes, but it only knows about abstract classes (or interfaces) that cannot instantiate.

    PROBLEM: The Application class only knows when it has to instantiate a new Object of an abstract class, but not the specific type of subclass.

  • A superclass may require its subclasses to specify the Objects to be created at the creation time.

  • A class may delegate responsibility to one of several assisting subclasses so that services can be distributed locally to specific subclasses.


2. Solution: The Factory Method

The solution to the previous problem is a creational pattern, namely Factory Method.

A design pattern is a description or template for how to solve a problem that can be used in many different situations.

The Factory Method contains an interface which lets the subclasses decide at creation time which subclass to instantiate.

This pattern is named as a factory as it is responsible for manufacturing an Object. A factory does not make an Object. To make a new Object, we make a simple call to new. (Read “Java Review: Instantiation w/ the Keyword New”.

However, the Factory pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code. The use of factories in the code facilitates delegation of specific attributes of an object into specific subclasses. Therefore, the Factory pattern helps make a system independent of how objects are created.

“The Factory Method lets a class defer instantiation to subclasses.”


3. Structural Diagram

png

Diagram credit: Object-oriented systems analysis and design by Noushin Ashrafi


4. An Example with Java Objects

The example is based on Java. In this example, upon receiving the user choice of geometric shape, either Circle or Rectangle, the factory interface will defer the instantiation to the associated subclass.

Class Diagram

The following class diagram shows the structure of the classes and interface.

png


Their signatures are displayed in the following.

The Factory interface ShapeProcessor

ShapeProcessor is a factory interface, only containing a group of related methods with empty bodies.

1
	public interface ShapeProcessor 

The superclass Shape

Shape contains the common properties and operations that are shared among its subclasses, Circle and Rectangle.

1
	public class Shape

The subclass Circle that implements ShapeProcessor and extends Shape

Circle extends Shape to inherit all the members from Shape, and implements ShapeProcessor for its abstract methods.

1
	public class Circle extends Shape implements ShapeProcessor

The subclass Rectangle that implements ShapeProcessor and extends Shape

Rectangle extends Shape to inherit all the members from Shape, and implements ShapeProcessor for its abstract methods.

1
	public class Rectangle extends Shape implements ShapeProcessor

5. Implementing a Factory Interface in Java

A factory interface only contains a group of related methods with empty bodies. An interface method is separate from its implementation. The subclasses will be responsible for implementing the interfaces.

Both classes and interfaces can be organized into a package (a namespace).

The following is an implementation of the Factory interface ShapeProcessor by using the keyword interface. Noticeably, the interface class can also contain the constants.

1
2
3
4
5
6
7
8
9
10
11
12
13
package interfaces;

public interface ShapeProcessor {

    static final double PI = 3.14159;

    public void setFillColor(int redVal, int greenVal, int blueVal);

    public double calcArea();

    public double calcCircumference();

}

6. Instantiating New Objects at Runtime

With the class structure in the example, we can declare new objects and instantiate them in an Application class (Main Class) in the main method.

  • Declare a new object of the Factory Interface type.

  • Instantiate the new object by calling the constructor of the required subclass.

An example is given below for a sample main method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
    public static void main(String[] args) {

        ShapeProcessor myshape = null; // Declare a ShapeProcessor object

        String input = JOptionPane.showInputDialog(null, "Enter either circle or rectangle:", "Select a shape", 0);

        switch (input) {
            case "circle": // delegate instantiation to subclass Circle                
                myshape = new Circle(5.6);
                break;
            case "rectangle": // defer instantiation to subclass Rectangle
                myshape = new Rectangle(3.2, 5.1);
                break;
            default:
        }

        if (myshape != null) {
            double area = myshape.calcArea();
            JOptionPane.showMessageDialog(null, "Your " + input + " has an area of " + area + ".", "The area of your shape", 0);
        } else {
            JOptionPane.showConfirmDialog(null, input + "Sorry. Your choice doesn't exist in the system. Contact us.", null, 0);
        }
       

    }// End of Main method

7. The Sample Code

The sample code is provided to help you understand the Factory method, in the following five class files.

  • Factory Interface: ShapeProcessor

You can view the sample code: ShapeProcessor.java

  • Superclass: Shape

You can view the sample code: Shape.java

  • Subclass 1: Circle

You can view the sample code: Circle.java

  • Subclass 2: Rectangle

You can view the sample code: Rectangle.java

  • Application Class: Draw

You can view the sample code: Draw.java