codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

7 / 100 topics
5Introduction to Creational Patterns6Singleton Pattern7Factory Method Pattern8Abstract Factory Pattern9Builder Pattern10Prototype Pattern31Practical Exercises for Creational Patterns
Tutorials/Design Patterns/Factory Method Pattern
🎭Design Patterns

Factory Method Pattern

Updated 2026-05-15
10 min read

Factory Method Pattern

Introduction

The Factory Method pattern is one of the creational design patterns in software engineering. It provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. This pattern promotes loose coupling between the client code and the concrete classes it creates.

In this tutorial, we'll explore the Factory Method pattern, understand its components, and see how it can be implemented in real-world scenarios.

Concept

The core idea behind the Factory Method pattern is to delegate the responsibility of object creation to subclasses. This allows for more flexibility and scalability in your codebase. The pattern involves:

  1. Product: An interface or abstract class that defines the common methods for objects that are created.
  2. Concrete Product: Classes that implement the Product interface.
  3. Creator: An interface with a factory method that returns a Product object.
  4. Concrete Creator: Implements the Creator interface and provides an implementation of the factory method.

By using this pattern, you can create objects without specifying the exact class of object that will be created. This is particularly useful when you want to decouple the client code from the concrete classes it uses.

Examples

Let's dive into a practical example to illustrate how the Factory Method pattern works.

Example 1: Basic Implementation

Suppose we have an application that needs to create different types of documents (e.g., PDF, Word). We can use the Factory Method pattern to encapsulate the document creation logic.

Step 1: Define the Product Interface

First, let's define a Document interface with a common method for displaying the document type.

TypeScript
1interface Document {
2 display(): void;
3}

Step 2: Implement Concrete Products

Next, we'll create concrete classes that implement the Document interface.

TypeScript
1class PDFDocument implements Document {
2 display() {
3 console.log("Displaying a PDF document.");
4 }
5}
6
7class WordDocument implements Document {
8 display() {
9 console.log("Displaying a Word document.");
10 }
11}

Step 3: Define the Creator Interface

Now, let's define a Creator interface with a factory method.

TypeScript
1interface Creator {
2 createDocument(): Document;
3}

Step 4: Implement Concrete Creators

Finally, we'll implement concrete classes that provide specific implementations of the factory method.

TypeScript
1class PDFCreator implements Creator {
2 createDocument(): Document {
3 return new PDFDocument();
4 }
5}
6
7class WordCreator implements Creator {
8 createDocument(): Document {
9 return new WordDocument();
10 }
11}

Step 5: Use the Factory Method

We can now use these creators to create and display documents without specifying the exact class of document.

TypeScript
1function clientCode(creator: Creator) {
2 const document = creator.createDocument();
3 document.display();
4}
5
6const pdfCreator = new PDFCreator();
7clientCode(pdfCreator); // Output: Displaying a PDF document.
8
9const wordCreator = new WordCreator();
10clientCode(wordCreator); // Output: Displaying a Word document.

Example 2: Extending the Pattern

Let's extend our example by adding another type of document, say TextDocument.

Step 1: Implement the New Concrete Product

First, we'll create a new concrete class for the text document.

TypeScript
1class TextDocument implements Document {
2 display() {
3 console.log("Displaying a Text document.");
4 }
5}

Step 2: Implement the New Concrete Creator

Next, we'll create a new creator for the text document.

TypeScript
1class TextCreator implements Creator {
2 createDocument(): Document {
3 return new TextDocument();
4 }
5}

Step 3: Use the Extended Factory Method

We can now use the new creator to create and display a text document.

TypeScript
1const textCreator = new TextCreator();
2clientCode(textCreator); // Output: Displaying a Text document.

What's Next?

In this tutorial, we explored the Factory Method pattern, which allows you to create objects without specifying the exact class of object that will be created. This promotes loose coupling and makes your codebase more flexible.

Next, you can learn about the Abstract Factory Pattern, which extends the Factory Method pattern by providing a way to create families of related or dependent objects without specifying their concrete classes. This pattern is particularly useful when dealing with complex systems with multiple interdependent object types.

Stay tuned for more design patterns and best practices in software engineering!


PreviousSingleton PatternNext Abstract Factory Pattern

Recommended Gear

Singleton PatternAbstract Factory Pattern