The Abstract Factory and Factory Method design patterns are both creational patterns used to create objects, but they differ in their structure and intent. Here are the key differences:
👉👉👉 Factory Method Design Pattern in C#
👉👉👉 Abstract Factory Design Pattern in C# with Real-Time Example
1. Intent
- Factory Method: Defines an interface for creating a single product, but lets subclasses alter the type of product that will be created. The intention is to defer the instantiation of a class to subclasses.
- Abstract Factory: Provides an interface for creating families of related or dependent products without specifying their concrete classes. The intention is to create a set of related products.
2. Structure
Factory Method: Typically involves a single method in a class to create an object. This pattern relies on inheritance: a base class defines the factory method, and derived classes implement the factory method to create specific products.
// Factory Methodpublic abstract class Creator{ public abstract IProduct FactoryMethod();}
public class ConcreteCreator : Creator{ public override IProduct FactoryMethod() { return new ConcreteProduct(); }}
public interface IAbstractFactory{ IProductA CreateProductA(); IProductB CreateProductB();}
public class ConcreteFactory : IAbstractFactory{ public IProductA CreateProductA() { return new ConcreteProductA(); }
public IProductB CreateProductB() { return new ConcreteProductB(); }}
3. Scope
- Factory Method: Deals with the creation of a single product. Each factory method is responsible for creating a single type of product.
- Abstract Factory: Deals with the creation of families of related products. An abstract factory groups related factory methods together, so you can create various related products through a single interface.
4. Usage
- Factory Method: Used when a class cannot anticipate the class of objects it must create. It's useful when a class wants its subclasses to specify the object to be created.
- Abstract Factory: Used when a system should be independent of how its products are created and composed. It's useful when a system needs to support multiple families of products.
5. Example
Factory Method
// Product Interfacepublic interface IProduct{ void Operation();}
// Concrete Productpublic class ConcreteProduct : IProduct{ public void Operation() { Console.WriteLine("ConcreteProduct Operation"); }}
// Creatorpublic abstract class Creator{ public abstract IProduct FactoryMethod();
public void AnOperation() { IProduct product = FactoryMethod(); product.Operation(); }}
// Concrete Creatorpublic class ConcreteCreator : Creator{ public override IProduct FactoryMethod() { return new ConcreteProduct(); }}
// Usageclass Program{ static void Main(string[] args) { Creator creator = new ConcreteCreator(); creator.AnOperation(); }}
Abstract Factory
// Abstract Productspublic interface IProductA{ void OperationA();}
public interface IProductB{ void OperationB();}
// Concrete Productspublic class ConcreteProductA1 : IProductA{ public void OperationA() { Console.WriteLine("ConcreteProductA1 OperationA"); }}
public class ConcreteProductB1 : IProductB{ public void OperationB() { Console.WriteLine("ConcreteProductB1 OperationB"); }}
// Abstract Factorypublic interface IAbstractFactory{ IProductA CreateProductA(); IProductB CreateProductB();}
// Concrete Factorypublic class ConcreteFactory1 : IAbstractFactory{ public IProductA CreateProductA() { return new ConcreteProductA1(); }
public IProductB CreateProductB() { return new ConcreteProductB1(); }}
// Usageclass Program{ static void Main(string[] args) { IAbstractFactory factory = new ConcreteFactory1(); IProductA productA = factory.CreateProductA(); IProductB productB = factory.CreateProductB();
productA.OperationA(); productB.OperationB(); }}
Summary
- Factory Method is used for creating a single product and allows subclasses to determine the actual product to be created.
- Abstract Factory is used for creating families of related products and provides a way to encapsulate a group of individual factories.
Each pattern has its own use case and choosing between them depends on the specific requirements of the software design.
👉👉👉When to use it Abstract Factory Design Pattern
EmoticonEmoticon