Factory Method Design Pattern

The Factory Method Design Pattern is one of the Creational Design Patterns. It provides a way to create objects without specifying their exact concrete classes in the client code.

Instead of creating objects directly using the new operator, object creation is delegated to a factory method. The factory decides which concrete object to create and returns it through a common interface.

This approach reduces coupling between object creation and object usage, making applications easier to extend and maintain. When new object types are introduced, the client code usually remains unchanged because it interacts only with the common interface.

This document presents two practical examples demonstrating the implementation of the Factory Method Design Pattern in C++.

Example 1: Logistics Transport System

This example demonstrates a logistics application where different transportation methods are created through a factory instead of being instantiated directly by the client.

Program


Explanation

The TransportFactory class is responsible for creating transport objects.

The client requests a transport object by providing a transport type, but it never creates a Truck or Ship directly.

The factory returns an object through the common ITransportVehicle interface, allowing the client to work with different transport types using the same code.

If a new transport type such as Airplane is added in the future, only the factory needs to be extended while the client code remains unchanged.

This demonstrates the Factory Method Design Pattern.

Example 2: User Interface Button Factory

This example demonstrates a graphical user interface framework where buttons for different operating systems are created through a factory.

Program


Explanation

The ButtonFactory class is responsible for creating platform-specific button objects.

The client only requests a button for a particular operating system and receives an object through the common IButton interface.

The client never needs to know whether the returned object is a WindowsButton or a MacButton.

If support for another operating system such as Linux is required, a new button class can be created and the factory can be extended accordingly without modifying the client code.

This demonstrates the Factory Method Design Pattern.

Characteristics of the Factory Method Design Pattern

PropertyDescription
Pattern TypeCreational Design Pattern
Core PrincipleDelegate object creation to a factory instead of creating objects directly in the client code.
ImplementationUses a factory method that returns objects through a common interface or abstract class.
Main BenefitReduces coupling and makes applications easier to extend with new object types.
Common ApplicationsUI frameworks, database drivers, payment gateways, logging systems, and transportation management systems.
Design BenefitSupports the Open-Closed Principle by allowing new product types to be added with minimal changes to existing code.

Conclusion

The Factory Method Design Pattern separates object creation from object usage by introducing a dedicated factory responsible for instantiation. This reduces dependencies on concrete classes and improves flexibility, scalability, and maintainability. As demonstrated in the Logistics Transport System and User Interface Button Factory examples, the Factory Method pattern enables applications to support new object types with minimal modifications to existing code.