Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Factory Method Design Pattern

DZone's Guide to

Factory Method Design Pattern

Check out one of the most widely used creational patterns.

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Factory method pattern enables us to create an object without exposing the creation logic to the client and refer to the newly-created object using a common interface. It is one of the most widely used creational patterns.

This pattern is also known as the Virtual Constructor.

The intent of this pattern, according to Design Patterns by Gamma et al, is to:

  • Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method allows a class to defer instantiation to subclasses.

The Factory method is for creating objects. A superclass specifies all standard and generic behavior and then delegates the creation details to subclasses that are supplied by the client.

The Factory method makes a design more customizable and only a little more complicated. Other design patterns require new classes, whereas Factory only requires a new operation.

Structure

The structure of factory method pattern is as shown in the figure below:

factory-method-pattern-structure

Figure: Structure of Factory Method Pattern

Nowadays, a static method of a class is popularly used as a factory method that returns an object of the required class' type. Unlike a constructor, the actual object it returns might be an instance of a subclass and also an existing object might be reused, instead of a new object created.

One of the advantages of using a factory instead of a constructor is that factory methods can have different and more descriptive names.

Example

Let us consider an application that draws different geometric shapes on the basis of clients' demand. The class diagram of the application is as shown below:

factory-method-pattern-example

Figure: Factory Method Pattern Example

The ShapeFactory class has a static method getShape(), which returns the GeometricShape object on the basis of string name input.

Java Implementation

We've presented the Java implementation of the application discussed above.

Let's first create an interface for a product to be manufactured by thefactory.

/**
 * Product interface
 */
public interface GeometricShape {
    void draw();
}


The following are the implementations of the above interface:

/**
 * Concrete Product
 */
public class Line implements GeometricShape {
    @Override
    public void draw() {
        System.out.println("Line Drawn.");
    }
}
/**
 * Concrete Product
 */
public class Rectangle implements GeometricShape {
    @Override
    public void draw() {
        System.out.println("Rectangle is drawn.");
    }
}
/**
 * Concrete product
 */
public class Circle implements GeometricShape{
    @Override
    public void draw() {
        System.out.println("Circle is drawn.");
    }
}


I have added the following enums to name the shapes:

public enum ShapeType {
    LINE,
    CIRCLE,
    RECTANGLE,
    TRIANGLE
}


Now, let's create a factory that provides the product (in this case, GeometricShape):

/**
 * Concrete Product
 */
public abstract class ShapeFactory {
    public static GeometricShape getShape(ShapeType name) {
        GeometricShape shape = null;
        switch (name) {
            case LINE:
                shape = new Line();
                break;
            case CIRCLE:
                shape = new Circle();
                break;
            case RECTANGLE:
                shape = new Rectangle();
                break;
        }
        return shape;
    }
}


The client for the application provides the name of the shape required as follow.

/**
 * Client
 */
public class Application {
    public static void main(String[] args) {
        //requests for circle shape
        GeometricShape circle = ShapeFactory.getShape(ShapeType.CIRCLE);
        if (circle != null) {
            circle.draw();
        } else {
            System.out.println("This shape can not be drawn.");
        }
        //requests non existent shape
        GeometricShape triangle = ShapeFactory.getShape(ShapeType.TRIANGLE);
        if (triangle != null) {
            triangle.draw();
        } else {
            System.out.println("This shape can't be drawn");
        }
    }
}


The output of the program is:

Circle is drawn.
This shape can't be drawn


Since there exists a circle shape, a valid Circle object is returned. But, there is no class with a triangle, so the shape can't be drawn.

Conclusion

This post talked about the summarized form of the Factory method pattern, as one of the GOF patterns, with a simple example. 

The source code for all example presented above is available on GitHub.

Happy coding!

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

Topics:
factory method pattern ,desgin patterns ,java ,code ,tutorial ,factory ,shapefactory ,virtual constructor

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}