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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Design Pattern: What You Need to Know to Improve Your Code Effectively
  • Prototype Pattern in JavaScript
  • Object Relational Behavioral Design Patterns in Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns

Trending

  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • Subtitles: The Good, the Bad, and the Resource-Heavy
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • AI, ML, and Data Science: Shaping the Future of Automation
  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.

By 
Yogen Rai user avatar
Yogen Rai
·
Nov. 20, 18 · Tutorial
Likes (17)
Comment
Save
Tweet
Share
60.3K 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.

Related

  • Design Pattern: What You Need to Know to Improve Your Code Effectively
  • Prototype Pattern in JavaScript
  • Object Relational Behavioral Design Patterns in Java
  • Distribution Design Patterns in Java - Data Transfer Object (DTO) And Remote Facade Design Patterns

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!