DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Application Architecture Design Principles
  • Top Six React Development Tools

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Application Architecture Design Principles
  • Top Six React Development Tools
  1. DZone
  2. Coding
  3. Languages
  4. Factory Method Design Pattern

Factory Method Design Pattern

Check out one of the most widely used creational patterns.

Yogen Rai user avatar by
Yogen Rai
CORE ·
Nov. 20, 18 · Tutorial
Like (17)
Save
Tweet
Share
58.43K Views

Join the DZone community and get the full member experience.

Join For Free

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!

Factory (object-oriented programming) Design Factory method pattern Object (computer science)

Opinions expressed by DZone contributors are their own.

Trending

  • How To Manage Vulnerabilities in Modern Cloud-Native Applications
  • DevOps Midwest: A Community Event Full of DevSecOps Best Practices
  • Application Architecture Design Principles
  • Top Six React Development Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: