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
public abstract class AbstractVehicleSaleProduct
{
public abstract Features VehicleFeatures();
public abstract Specification VehicleSpecification();
}
CarRentalProduct: defines a car rental object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class carRentroduct:AbstractVehicleRentProduct
{
string carModel;
public carRentroduct(string ModelName)
{
carModel = ModelName;
}
public override Features VehicleFeatures()
{
Features f = new Features();
return f;
}
public override Specification VehicleSpecification() {
Specification f = new Specification(carModel);
return f;
}
public override RegistrationDetails CarRegistration()\ {
RegistrationDetails r = new RegistrationDetails()
return r;
}
}
FourbyFourRentalProduct: defines a 4x4 rental object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class fourbyfourRentProduct : AbstractVehicleRentProduct
{
string carModel;
public fourbyfourRentProduct(string ModelName)
{
carModel = ModelName;
}
public override Features VehicleFeatures()
{
//return items using repository pattern
Features f = new Features();
return f; }
public override RegistrationDetails CarRegistration()
{
RegistrationDetails r = new RegistrationDetails();
return r;
}
public override Specification VehicleSpecification()
{
//return items using repository pattern
Specification f = new Specification(carModel);
return f;
}
}
JeepRentalProduct: defines a car rental object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class JeepRentProduct : AbstractVehicleRentProduct
{
string carModel;
public JeepRentProduct(string ModelName)
{
carModel = ModelName;
}
public override Features VehicleFeatures()
{
Features f = new Features();
return f;
}
public override Specification VehicleSpecification()
{
Specification f = new Specification(carModel);
return f;
}
public override RegistrationDetails CarRegistration()
{
RegistrationDetails r = new RegistrationDetails();
return r;
}
}
CarSaleProduct: defines a car sales object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class carSaleProduct:AbstractVehicleSaleProduct
{
string carModel;
Features myfutures = new Features();
public carSaleProduct(string ModelName)
{
carModel = ModelName;
myfutures= VehicleFeatures();
VehicleSpecification();
}
public override Features VehicleFeatures()
{//need to replace by querying using the model name
List<string> color = new List<string>() { "Red", "Gold", "Silver", "white" };
IEnumerable<string> colors = color.AsEnumerable();
Features f = new Features() { DoorNumbers = 4, Color = color, Seats = 5 };
return f; }
public override Specification VehicleSpecification() {
Specification f = new Specification(carModel) { IsAutomaticAvailable = true, IsDieselAvailable = false, EndPrice=90000, StartingPrice=3000 };
return f;
}
}
FourbyFourSaleProduct: defines a 4x4 sales object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class fourbyfourSaleProduct : AbstractVehicleSaleProduct
{
string carModel;
public fourbyfourSaleProduct(string ModelName)
{
carModel = ModelName;
}
public override Features VehicleFeatures()
{
Features f = new Features();
return f;
}
public override Specification VehicleSpecification()
{| Specification f = new Specification(carModel);
return f;
}
JeepSaleProduct: defines a Jeep sales object to be created by the corresponding concrete factory and implements the "AbstractVehicleRent" interface
public class JeepSaleProduct : AbstractVehicleSaleProduct
{
string carModel;
public JeepSaleProduct(string ModelName)
{
carModel = ModelName;|
}
public override Features
ehicleFeatures()
{
Features f = new Features();
return f;
}
public override Specification VehicleSpecification()
{
Specification f = new Specification(carModel);
return f;
}
}
Client: uses interfaces declared by the "AbstractCarFactory" , "AbstractVehicleRent" and "AbstractVehicleSale" classes
public class Client
{
private IEnumerable<AbstractVehicleSaleProduct> AbstractProductA;
private IEnumerable<AbstractVehicleRentProduct> AbstractProductB;
// Constructor
public Client(AbstractCarFactory factory)
{
AbstractProductB = factory.GetVehicleRent();
AbstractProductA = factory.GetVehicleSale();
}
//useful to pass vehicle name to main function
public static class StaticCarModels
{
public enum Nissan
{
Almera,
Micro,
BludBird
}
public enum Ford
{
Pinto,
Fiesta,
Focus
}
public enum Vauxhall
{
Corsa,
Belmont,
Tigra
}
}
//Main function
private void invokeme()
{
bstractCarFactory Micro = new NissanFactory(StaticCarModels.Nissan.Micro.ToString());
lient c1 = new Client(Micro);
}