DevIQ
Factory Method Pattern
Factory Method Pattern
The Factory Method pattern is an object creation pattern from the Design Patterns book, by the so-called Gang of Four (GoF). It enables us to define an interface or abstract class for creating an object, leaving the specific details to the implementations. The Factory Method pattern allows for loose coupling and enhanced flexibility with regards to creating objects in code. It also allows you to encapsulate the potential complexity of object creation.
The pattern works like this:
- Create an interface or abstract base class with a method for creating an object.
- Implement the interface or inherit from an abstract base class and define the abstract methods.
- Swap implementations as needed.
Here is an example of setting up interfaces and classes with the Factory Method pattern:
using System;
interface IProductFactory { IProduct CreateProduct();}
class MentoringFactory : IProductFactory { public IProduct CreateProduct() { return new MentoringOpportunity(); }}
class TrainingFactory : IProductFactory { public IProduct CreateProduct() { return new TrainingOffering(); }}
interface IProduct { void DisplayDetails();}
class MentoringOpportunity : IProduct { public void DisplayDetails() { Console.WriteLine("This is a mentoring opportunity."); }}
class TrainingOffering : IProduct { public void DisplayDetails() { Console.WriteLine("This is a training offering."); }}
Now with this code, how are the factories used? Notice that the only thing that is changing here is the specific implementation of the factory.
class Program { static void Main() { IProductFactory productFactory = new MentoringFactory(); IProduct productItem = productFactory.CreateProduct(); productItem.DisplayDetails();
IProductFactory productFactory2 = new TrainingFactory(); IProduct productItem2 = productFactory2.CreateProduct(); productItem2.DisplayDetails(); }}
You can use dependency injection to use the factory interfaces where needed and then pass along specific implementations of the factories when needed.
For example, suppose we had a collection of web pages that display collections of products. The base web page for displaying products - BaseProductDisplay
- could have a `IProductFactory`` injected in. All product display pages would inherit from this page.
For the page that displays mentoring opportunities, the MentoringFactory
can be injected. For a separate page that displays the training offerings, the TrainingFactory
could be injected.