In this article, we will discuss Abstract Factory Design Patterns in C# with models. it is a type of Creational Design Pattern.
What is Abstract Factory Design Pattern?
According to the Gang of Four Definition: “The Abstract Factory Design Pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes“.
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes
Abstract Factory pattern belongs to creational patterns and is one of the most used design patterns in real-world applications
An abstract factory is a super factory that creates other factories
We need to Choose Abstract Factory Pattern when
- The application needs to create multiple families of objects or products
- We need to use only one of the subset of families of objects at a given point of time
- We want to hide the implementations of the families of products by decoupling the implementation of each of these operations
Understanding Abstract Factory Design Pattern:
- Client is a class that uses AbstractFactory and AbstractProduct interfaces to create a family of related objects.
- AbstractFactory is an interface that is used to create an abstract product.
- ConcreteFactory is a class that implements the AbstractFactory interface to create concrete products.
- AbstractProduct is an interface that declares a type of product.
- ConcreteProduct is a class that implements the AbstractProduct interface to create a product.
Permanent Employee
- Managerial Position is eligible for Apple MAC Book Laptop
- Non-Managerial Position is eligible for Apple iMac desktop
- Managerial Position is eligible for Dell Laptop
- Non-Managerial Position is eligible for Dell desktop
|
public class Employee { public int Id
{ get; set; }
public string
Name { get; set; } public string
Department { get; set; } public
Enumerations.EmployeeType EmployeeTypeID { get; set; }
public string
ComputerDetails { get; set; }
public
Enumerations.JobPosition EmployeeJobPosition { get; set; } } |
class BrandInterfaceFactory { public interface IBrand { string
GetBrand(); } }
class SystemTypeInterfaceFactory { public interface ISystemType { string
GetSystemType(); } }
class ProcessorInterfaceFactory { public interface IProcessor { string
GetProcessor(); }
} |
public class SystemType { public class Laptop :
ISystemType { public string
GetSystemType() { return
Enumerations.ComputerTypes.Laptop.ToString(); } } public class Desktop :
ISystemType { public string
GetSystemType() { return
Enumerations.ComputerTypes.Desktop.ToString(); } } } |
public class BrandType { public class MAC:
IBrand { public string
GetBrand() { return
Enumerations.Brands.APPLE.ToString(); } } }
public class DELL:
IBrand { public string
GetBrand() { return
Enumerations.Brands.DELL.ToString(); } } |
public class ProcesserType { public class I7 :
IProcessor { public string
GetProcessor() { return
Enumerations.Processors.I7.ToString(); } } public class I5 :
IProcessor { public string
GetProcessor() { return
Enumerations.Processors.I5.ToString(); } } } |
public interface IComputerFactory { IBrand
Brand(); ISystemType
SystemType(); IProcessor
Processor(); } |
public class DellFactory: IComputerFactory { public
IBrand Brand() { return new
DELL(); }
public
IProcessor Processor() { return new
ProcesserType.I5(); }
public virtual
ISystemType SystemType() { return new
SystemType.Desktop(); } }
public class DellLaptopFactory :
DellFactory { public override
ISystemType SystemType() { return new
SystemType.Laptop(); } } |
public class MACFactory: IComputerFactory { public
IBrand Brand() { return new
BrandType.MAC(); }
public
IProcessor Processor() { return new
ProcesserType.I7(); }
public virtual
ISystemType SystemType() { return new
SystemType.Desktop(); } } public class MACLaptopFactory:
MACFactory {
public override
ISystemType SystemType() { return new
SystemType.Laptop(); }
} |
public class EmployeeSystemFactory { public
IComputerFactory Create(Employee e) {
IComputerFactory returnValue = null; if
(e.EmployeeTypeID == Enumerations.EmployeeType.Permanent) { if
(e.EmployeeJobPosition == Enumerations.JobPosition.Manager) {
returnValue = new MACLaptopFactory(); } else {
returnValue = new MACFactory(); } } else if
(e.EmployeeTypeID == Enumerations.EmployeeType.Contract) { if
(e.EmployeeJobPosition == Enumerations.JobPosition.Manager) {
returnValue = new DellLaptopFactory(); } else
returnValue = new DellFactory(); } return
returnValue; } } |
public class EmployeeSystemManager { IComputerFactory
_IComputerFactory; public EmployeeSystemManager(IComputerFactory
iComputerFactory) {
_IComputerFactory = iComputerFactory; } public string
GetSystemDetails() { IBrand brand =
_IComputerFactory.Brand(); IProcessor
processor = _IComputerFactory.Processor(); ISystemType
systemType = _IComputerFactory.SystemType();
string
returnValue = string.Format("{0} {1} {2}", brand.GetBrand(),
systemType.GetSystemType(), processor.GetProcessor()); return
returnValue; } } |
static void
Main(string[] args) { Employee emp =
new Employee() { Id = 1,
EmployeeJobPosition = Enumerations.JobPosition.Manager,
EmployeeTypeID = Enumerations.EmployeeType.Permanent, };
IComputerFactory factory = new
EmployeeSystemFactory().Create(emp);
EmployeeSystemManager manager = new
EmployeeSystemManager(factory);
emp.ComputerDetails = manager.GetSystemDetails();
Console.WriteLine("Result :- " +
emp.ComputerDetails);
Console.ReadLine(); } |
Pointe to Remember:
- Abstract Factory Pattern provides an interface for creating families of related dependent objects without specifying their concrete classes.
- The Abstract Factory Pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.
- The abstract factory design pattern is merely an extension to the factory method pattern or factory pattern, which allows you to create objects without being concerned about the actual class of the object being created.
- Abstract means hiding some information and factory means which produces the products and pattern means a design. So, the Abstract Factory Pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme.
When to use it Abstract Factory Design Pattern?
When we need to use the Abstract Factory Design Pattern in the following cases
- When you want to create a set of related objects or dependent objects which must be used together.
- When the system should configure to work with multiple families of products.
- When the Concrete classes should be decoupled from the clients.
Differences between Abstract Factory and Factory Method Design Pattern:
- Abstract Factory Design Pattern adds a layer of abstraction to the Factory Method Design Pattern
- The Abstract Factory design pattern implementation can have multiple factory methods
- Similar products of a factory implementation are grouped in the Abstract factory
- The Abstract Factory Pattern uses object composition to decouple applications from specific implementations
- The Factory Method Pattern uses inheritance to decouple applications from specific implementations
1 comments:
Write commentsnice article
ReplyEmoticonEmoticon