We may earn an affiliate commission when you visit our partners.

Factory Method

Save

Factory Method is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It solves the problem of having to specify the exact class of an object that will be created, allowing for greater flexibility and code reusability.

Benefits of Factory Method

There are several benefits to using the Factory Method pattern:

  • Decouples client code from concrete classes: The client code does not need to know the specific class of the object that will be created. This allows for greater flexibility and makes it easier to change the type of object that is created.
  • Promotes code reusability: The Factory Method pattern can be used to create a family of related objects without having to duplicate code. This can make it easier to maintain and update your codebase.
  • Supports extensibility: The Factory Method pattern makes it easy to add new types of objects to your application without having to modify the existing code. This can be useful when you need to support new types of objects in the future.

How Factory Method Works

Read more

Factory Method is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It solves the problem of having to specify the exact class of an object that will be created, allowing for greater flexibility and code reusability.

Benefits of Factory Method

There are several benefits to using the Factory Method pattern:

  • Decouples client code from concrete classes: The client code does not need to know the specific class of the object that will be created. This allows for greater flexibility and makes it easier to change the type of object that is created.
  • Promotes code reusability: The Factory Method pattern can be used to create a family of related objects without having to duplicate code. This can make it easier to maintain and update your codebase.
  • Supports extensibility: The Factory Method pattern makes it easy to add new types of objects to your application without having to modify the existing code. This can be useful when you need to support new types of objects in the future.

How Factory Method Works

The Factory Method pattern works by defining an interface for creating objects. This interface is then implemented by subclasses that define the specific type of object that will be created. The client code then uses the factory method to create objects without having to specify the exact class of the object that will be created.

When to Use Factory Method

The Factory Method pattern is useful in the following situations:

  • When you need to create a family of related objects without having to specify the exact class of each object.
  • When you want to decouple client code from concrete classes.
  • When you want to promote code reusability.
  • When you want to support extensibility.

Example of Factory Method

Here is an example of how to use the Factory Method pattern in Java:

public interface ShapeFactory {
    Shape getShape(String shapeType);
}

public class ShapeFactoryImpl implements ShapeFactory {

    @Override
    public Shape getShape(String shapeType) {
        if (shapeType.equalsIgnoreCase("CIRCLE")) {
            return new Circle();
        } else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
            return new Rectangle();
        } else if (shapeType.equalsIgnoreCase("SQUARE")) {
            return new Square();
        } else {
            return null;
        }
    }
}

public class Shape {

    // Common methods for all shapes

}

public class Circle extends Shape {

    // Specific methods for circles

}

public class Rectangle extends Shape {

    // Specific methods for rectangles

}

public class Square extends Shape {

    // Specific methods for squares

}

public class Client {

    public static void main(String[] args) {
        ShapeFactory shapeFactory = new ShapeFactoryImpl();

        Shape circle = shapeFactory.getShape("CIRCLE");
        Shape rectangle = shapeFactory.getShape("RECTANGLE");
        Shape square = shapeFactory.getShape("SQUARE");

        // Use the shapes as needed
    }
}

Conclusion

The Factory Method pattern is a creational design pattern that provides an interface for creating objects but allows subclasses to alter the type of objects that will be created. It is a useful pattern when you need to create a family of related objects without having to specify the exact class of each object, when you want to decouple client code from concrete classes, when you want to promote code reusability, and when you want to support extensibility.

Share

Help others find this page about Factory Method: by sharing it with your friends and followers:

Reading list

We've selected nine books that we think will supplement your learning. Use these to develop background knowledge, enrich your coursework, and gain a deeper understanding of the topics covered in Factory Method.
Chapter from the seminal book 'Design Patterns: Elements of Reusable Object-Oriented Software'. It provides a detailed overview of the Factory Method pattern.
Provides a comprehensive overview of enterprise application architecture patterns, including the Factory Method pattern. It is written by Martin Fowler, one of the world's leading experts on software design.
Provides a comprehensive overview of refactoring, a technique for improving the design of existing code. It includes a chapter on how to use the Factory Method pattern to refactor code.
Tutorial on design patterns, with a focus on making them easy to understand and use. It includes a chapter on the Factory Method pattern.
Provides a comprehensive overview of design patterns in C#, a popular programming language for developing enterprise applications. It includes a chapter on the Factory Method pattern.
Our mission

OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.

Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.

Find this site helpful? Tell a friend about us.

Affiliate disclosure

We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.

Your purchases help us maintain our catalog and keep our servers humming without ads.

Thank you for supporting OpenCourser.

© 2016 - 2024 OpenCourser