Abstract Factory Pattern
The Abstract Factory Pattern is a software creational design pattern that provides a way to encapsulate a group of individual factories that have a common theme without specifying the concrete classes. The client does not know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interface of their products. This pattern separates the details of implementation of a set of objects from their general usage and relies on object composition, since object creation is implemented in methods exposed in the factory interface.
AbstractCarFactory: declares an interface for operations that create abstract products
public abstract class AbstractCarFactory
{
//public CarFeatures();
public abstract IEnumerable<AbstractVehicleSaleProduct> GetVehicleSale();
public abstract IEnumerable<AbstractVehicleRentProduct> GetVehicleRent();
}
FordFactory: implements the operations to create concrete product objects relevant to Ford.
public class FordFactory : AbstractCarFactory
{
string name;
public FordFactory(string modelName)
{
name = modelName;
}
public override IEnumerable<AbstractVehicleSaleProduct> GetVehicleSale()
{
//Ford has 4x4, car only
List<AbstractVehicleSaleProduct> myList = new List<AbstractVehicleSaleProduct>();
myList =(List<AbstractVehicleSaleProduct>) ReturnList(); return myList;
}
IEnumerable<Object> ReturnList()
{
List<Object> myList = new List<Object>();
AbstractVehicleSaleProduct vehicleProduct = new carRentroduct(name);
myList.Add(vehicleProduct);
vehicleProduct = new fourbyfourRentProduct(name);
myList.Add(vehicleProduct);
//vehicleProduct = new JeepRentProduct(name);
//myList.Add(vehicleProduct);
return myList;
}
public override IEnumerable<AbstractVehicleRentProduct> GetVehicleRent()
{
List<AbstractVehicleRentProduct> myList = new List<AbstractVehicleRentProduct>();
myList = (List<AbstractVehicleRentProduct>)ReturnList();
return myList;
}
}
NissanFactory: implements the operations to create concrete product objects relevant to Nissan.
public class NissanFactory : AbstractCarFactory
{
string name;
public NissanFactory(string modelName)
{
name = modelName;
}
public override IEnumerable<AbstractVehicleSaleProduct> GetVehicleSale()
{
//Nissan has car & jeep only
// List<AbstractVehicleSaleProduct> myList = new List<AbstractVehicleSaleProduct>();
List<AbstractVehicleSaleProduct> myList = new List<AbstractVehicleSaleProduct>();
AbstractVehicleSaleProduct vehicleProduct = new carSaleProduct(name);
myList.Add(vehicleProduct);
//vehicleProduct = new fourbyfourRentProduct(name);
//myList.Add(vehicleProduct);
vehicleProduct = new JeepRentProduct(name);
myList.Add(vehicleProduct);
return myList;
}
public override IEnumerable<AbstractVehicleRentProduct> GetVehicleRent()
{
List<AbstractVehicleRentProduct> myList = new List<AbstractVehicleRentProduct>();
AbstractVehicleRentProduct vehicleProduct = new carRentroduct(name);
vehicleProduct.VehicleFeatures();
vehicleProduct.VehicleSpecification();
myList.Add(vehicleProduct);
//vehicleProduct = new fourbyfourRentProduct(name);
//myList.Add(vehicleProduct);
vehicleProduct = new JeepRentProduct(name);
myList.Add(vehicleProduct);
return myList;
}
}
VauxhallFactory: implements the operations to create concrete product objects
relevant to Vauxhall.
public class VauxhallFactory : AbstractCarFactory
{
string name;
public VauxhallFactory(string modelName)
{
name = modelName;
}
public override IEnumerable<AbstractVehicleSaleProduct> GetVehicleSale()
{
//Ford has all items
List<AbstractVehicleSaleProduct> myList = new List<AbstractVehicleSaleProduct>();
myList = (List<AbstractVehicleSaleProduct>)ReturnList();
return myList;
}
IEnumerable<Object> ReturnList()
{
List<Object> myList = new List<Object>();
AbstractVehicleSaleProduct vehicleProduct = new carRentroduct(name);
myList.Add(vehicleProduct);
vehicleProduct = new fourbyfourRentProduct(name);
myList.Add(vehicleProduct);
vehicleProduct = new JeepRentProduct(name);
myList.Add(vehicleProduct);
return myList;
}
public override IEnumerable<AbstractVehicleRentProduct> GetVehicleRent()
{
List<AbstractVehicleRentProduct> myList = new List<AbstractVehicleRentProduct>();
myList = (List<AbstractVehicleRentProduct>)ReturnList();
return myList;
}
}
AbstractVehicleRentProduct: declares an interface for a type of product object for VehicleRent
public abstract class AbstractVehicleRentProduct : AbstractVehicleSaleProduct
{
public abstract RegistrationDetails CarRegistration();
}
AbstractVehicleSaleProduct: declares an interface for a type of product object for VehicleSale

Dinesh GabhanePosted Apr 29, 2020, 11:36 PM
Buddy, This is what I was searching from long time. Awesome explanation, Great example. Truly superb article. Thanks for sharing. Keep writing
Munesh SharmaPosted Jun 6, 2017, 3:00 AM
Nice explanation .....(y)