Welcome back! So far, you’ve learned about the Singleton Pattern and have seen how it ensures a class has only one instance with a global access point. Now, we’re moving on to another essential creational design pattern: the Factory Method Pattern. This pattern is all about creating objects in a much more flexible way than direct instantiation. You’ll learn how to implement your own factory methods to instantiate different types of objects and see how this pattern allows your code to handle new object types with ease.
The Factory Method Pattern is a creational design pattern that provides an interface for creating an object but allows subclasses to alter the type of objects that will be created. This pattern promotes loose coupling by eliminating the need to specify the exact class of the object that will be created. Instead, the instantiation is handled by subclasses.
You should consider using the Factory Method Pattern when object creation requires conditional logic, when working with large class hierarchies, or when developing frameworks and libraries that need to allow users to extend and customize object creation.
To understand how the Factory Method Pattern is implemented, let's break the process down into intermediate steps.
First, define an abstract base class Document
with an abstract method Open
. This class will serve as the template for different types of documents.
C#1// Abstract class representing a Document 2abstract class Document 3{ 4 // Abstract method to be implemented by derived classes 5 public abstract void Open(); 6}
Next, create concrete subclasses of Document
. Each subclass will implement the Open
method. For example, let's define WordDocument
and ExcelDocument
.
C#1// WordDocument class inheriting from Document 2class WordDocument : Document 3{ 4 // Implementation of the Open method for Word documents 5 public override void Open() => Console.WriteLine("Opening Word document."); 6} 7 8// ExcelDocument class inheriting from Document 9class ExcelDocument : Document 10{ 11 // Implementation of the Open method for Excel documents 12 public override void Open() => Console.WriteLine("Opening Excel document."); 13}
Now, define an abstract base class DocumentCreator
with an abstract method CreateDocument
. This class will provide the interface for creating documents.
C#1// Abstract class representing a Document Creator 2abstract class DocumentCreator 3{ 4 // Abstract method to be implemented by derived classes 5 public abstract Document CreateDocument(); 6}
Create concrete subclasses of DocumentCreator
. Each subclass will implement the CreateDocument
method to instantiate and return a specific type of document.
C#1// WordDocumentCreator class inheriting from DocumentCreator 2class WordDocumentCreator : DocumentCreator 3{ 4 // Implementation of the CreateDocument method to create a Word document 5 public override Document CreateDocument() => new WordDocument(); 6} 7 8// ExcelDocumentCreator class inheriting from DocumentCreator 9class ExcelDocumentCreator : DocumentCreator 10{ 11 // Implementation of the CreateDocument method to create an Excel document 12 public override Document CreateDocument() => new ExcelDocument(); 13}
Finally, use the factory methods to create document objects without specifying their concrete classes.
C#1class Program 2{ 3 static void Main() 4 { 5 // Create a Word document using WordDocumentCreator 6 DocumentCreator creator = new WordDocumentCreator(); 7 Document doc = creator.CreateDocument(); 8 doc.Open(); // Output: Opening Word document. 9 10 // Create an Excel document using ExcelDocumentCreator 11 creator = new ExcelDocumentCreator(); 12 doc = creator.CreateDocument(); 13 doc.Open(); // Output: Opening Excel document. 14 } 15}
One of the key advantages of the Factory Method Pattern is its flexibility and extensibility. For instance, adding a new type of document, such as a PdfDocument
, does not require changes to the existing DocumentCreator
or Document
classes. Instead, you can create a new subclass of Document
and a corresponding subclass of DocumentCreator
.
C#1// PdfDocument class inheriting from Document 2class PdfDocument : Document 3{ 4 // Implementation of the Open method for PDF documents 5 public override void Open() => Console.WriteLine("Opening PDF document."); 6} 7 8// PdfDocumentCreator class inheriting from DocumentCreator 9class PdfDocumentCreator : DocumentCreator 10{ 11 // Implementation of the CreateDocument method to create a PDF document 12 public override Document CreateDocument() => new PdfDocument(); 13}
This extensibility makes the pattern ideal for applications where new types of objects are frequently added. You can introduce new products without altering the existing structure, enhancing the maintainability and evolvability of your codebase.
The Factory Method Pattern is essential because it promotes flexibility and scalability in your code designs. By delegating the creation of objects to factory methods, you can easily introduce new types of objects without changing the existing code, leading to better maintainability. Whether you're developing software libraries, frameworks, or complex applications, this pattern helps you manage and scale object creation efficiently. Ready to enhance your coding skills? Let's delve into the Factory Method Pattern and see how you can apply it to write cleaner, more maintainable code. Let's start!