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();
}
}



Join the conversation! Your thoughts help the community grow.