The Abstract Factory Design Pattern, according to the Gang of Four Definition, offers a means of encapsulating a collection of factories with a shared concept without defining their specific classes.
Pattern denotes a design, factory denotes the location of product production, and abstract denotes the concealment of certain information. Thus, a software design pattern known as the Abstract Factory Pattern offers a means of encapsulating a collection of distinct factories that have a common theme.
Put more succinctly, the Abstract Factory can be thought of as a super factory that produces other factories. The Factory of Factories is another name for this Abstract Factory. This means that while the concrete factory classes handle the actual object production, the Abstract Factory design pattern offers an interface for building families of linked or dependent products.
When a system must be independent of the creation, composition, and representation of its results, this design can be helpful. The idea is to have a factory method that returns an instance of one of these subclasses and a superclass that has several subclasses.
Parts of the Pattern for an Abstract factory
The components of the Abstract Factory Design Pattern are as follows.
- AbstractFactory: An interface for operations that produce abstract products is declared by the AbstractFactory. This will serve as an interface for the processes that generate objects of the Abstract Product.
- ConcreteFactory: ConcreteFactory carries out the processes necessary to produce concrete product objects. These classes offer implementations for the interface methods as well as the Abstract Factory interface. Concrete product objects can be created with the help of these concrete classes.
- AbstractProduct: Describes an interface for a certain kind of product object. These are going to be products' creation interfaces. In this case, we must specify the Operations that a Product ought to have.
- ConcreteProduct: The AbstractProduct interface is implemented by ConcreteProduct. The classes listed above are those that carry out the Abstract Product interface.
- Client: Interfaces declared by the AbstractFactory and AbstractProduct classes are utilized by the client. This class will build a family of products by utilizing our Abstract Factory and Abstract Product interfaces.
C# Implementation code
Creating abstract products
Here is the code for the IBike.cs file.
public interface IBike
{
void GetDetails();
}
Here is the code for the ICar. cs file.
public interface ICar
{
void GetDetails();
}
Creating Concrete Products
Here is the code for the RegularBike.cs file.
public class RegularBike : IBike
{
public void GetDetails()
{
Console.WriteLine("Bring Regular Bike Details...");
Console.WriteLine();
}
}
Here is the code for the SportsBike.cs file.
public class SportsBike : IBike
{
public void GetDetails()
{
Console.WriteLine("Bring Sports Bike Details...");
Console.WriteLine();
}
}
Here is the code for the RegularCar.cs file.
public class RegularCar : ICar
{
public void GetDetails()
{
Console.WriteLine("Bring Regular Car Details...");
Console.WriteLine();
}
}
Here is the code for the SportsCar.cs file.
public class SportsCar : ICar
{
public void GetDetails()
{
Console.WriteLine("Bring Sports Car Details...");
Console.WriteLine();
}
}
Creating Abstract Factory
Here is the code for the IVehicleFactory.cs file.
public interface IVehicleFactory
{
IBikeCreateBike();
ICar CreateCar();
}
Creating Concrete Factories
Here is the code for the RegularVehicleFactory.cs file.
public class RegularVehicleFactory : IVehicleFactory
{
public IBikeCreateBike()
{
return new RegularBike();
}
public ICar CreateCar()
{
return new RegularCar();
}
}
Here is the code for the SportsVehicleFactory.cs file.
public class SportsVehicleFactory : IVehicleFactory
{
public IBikeCreateBike()
{
return new SportsBike();
}
public ICar CreateCar()
{
return new SportsCar();
}
}
Client
IVehicleFactoryregularVehicleFactory = new RegularVehicleFactory();
IBikeregularBike = regularVehicleFactory.CreateBike();
regularBike.GetDetails();
ICar regularCar = regularVehicleFactory.CreateCar();
regularCar.GetDetails();
IVehicleFactorysportsVehicleFactory = new SportsVehicleFactory();
IBikesportsBike = sportsVehicleFactory.CreateBike();
sportsBike.GetDetails();
ICar sportsCar = sportsVehicleFactory.CreateCar();
sportsCar.GetDetails();
Output
Here are the abstract factory components that we are using as an illustration.
// Client: Main Method of Program Class
// Abstract Product A: IBike.cs
// Abstract Product B: ICar.cs
// ProductA1: RegularBike.cs
// ProductB1: SportsBike.cs
// ProductA2: RegularCar.cs
// ProductB2: SportsCar.cs
// Abstract Factory: IVehicleFactory.cs
// Concrete Factory1: RegularVehicleFactory.cs
// Concrete Factory2: SportsVehicleFactory.cs
As an illustration, suppose you need to add one more category like Customize car and bike details.
Here is the code for the ICustomizeBike.cs file.
public interface ICustomizeBike
{
void GetDetails();
}
Here is the code for the ICustomizeCar.cs file.
public interface ICustomizeCar
{
void GetDetails();
}
Here is the code for the CustomizeRegularBike.cs file.
public class CustomizeRegularBike : ICustomizeBike
{
public void GetDetails()
{
Console.WriteLine("Bring Customize Regular Bike Details...");
Console.WriteLine();
}
}
Here is the code for the CustomzieRegularCar.cs file.
public class CustomizeRegularCar : ICustomizeCar
{
public void GetDetails()
{
Console.WriteLine("Bring Customize Regular Car Details...");
Console.WriteLine();
}
}
Here is the code for the CustomizeSportsBike.cs file.
public class CustomizeSportsBike : ICustomizeBike
{
public void GetDetails()
{
Console.WriteLine("Bring Customize Sports Bike Details...");
Console.WriteLine();
}
}
Here is the code for the CustomizeSportsCar.cs file.
public class CustomizeSportsCar : ICustomizeCar
{
public void GetDetails()
{
Console.WriteLine("Bring Customize Sports Car Details...");
Console.WriteLine();
}
}
Here is the code for the IVehicleFactory.cs file.
public interface IVehicleFactory
{
ICustomizeBikeCreateCustomizeBike();
ICustomizeCarCreateCustomizeCar();
}
Here is the code for the RegularVehicleFactory.cs file.
public class RegularVehicleFactory : IVehicleFactory
{
public ICustomizeBikeCreateCustomizeBike()
{
return new CustomizeRegularBike();
}
public ICustomizeCarCreateCustomizeCar()
{
return new CustomizeRegularCar();
}
}
Here is the code for the SportsVehicleFactory.cs file.
public class SportsVehicleFactory : IVehicleFactory
{
public ICustomizeBikeCreateCustomizeBike()
{
return new CustomizeSportsBike();
}
public ICustomizeCarCreateCustomizeCar()
{
return new CustomizeSportsCar();
}
}
Here is the code for the Program.cs file.
ICustomizeBikeregularCustomizeBike = regularVehicleFactory.CreateCustomizeBike();
regularCustomizeBike.GetDetails();
ICustomizeCarregularCustomizeCar = regularVehicleFactory.CreateCustomizeCar();
regularCustomizeCar.GetDetails();
ICustomizeBikesportsCustimzeBike = sportsVehicleFactory.CreateCustomizeBike();
sportsCustimzeBike.GetDetails();
ICustomizeCarsportsCustomizeCar = sportsVehicleFactory.CreateCustomizeCar();
sportsCustomizeCar.GetDetails();
Output
When should I use it?
Make a collection of dependent or linked objects that need to be used in tandem.
The system ought to be set up to function with several product families.
The systems that are used should not affect how objects are created.
It is best to separate concrete classes from clients.
We learned the new technique and evolved together.
Happy coding!