Reputation: 141
I'm currently implementing the abstract factory pattern and I would like to know how would I implement it for this design.
I want to use the abstract factory pattern to help modify vehicle information. This information is generated for customer vehicles when requested. The generation occurs based on the type of vehicle.
An example of an object would look like this. (This would be multiple different objects like this for other types like Toyota, Audi, etc..)
public class BMW {
string Year;
string Make;
string Model;
}
I want to modify this object and add kilometers for example. This modification only happens if requested. Therefore, I want to use the abstract factory to help deal with this.
This is what I had so far
interface IModifyVehicle
{
public abstract BMWWithKM Modify(BMW bmw);
}
public class ModifyWithKM : IModifyVehicle
{
public override BMWWithKM Modify(BMW bmw)
{
//Add KM here
}
}
public class BMWWithKM {
string Year;
string Make;
string Model;
string KiloMeters;
public BMWWithKM(BMW bmw){
//Assign values
}
}
How would I integrate the factory pattern with this if I want to deal with the other type of vehicles? Also let's say if I want to make an class where I can modify the vehicle information with miles instead of kilometers. How could I integrate that with this?
Upvotes: 1
Views: 971
Reputation: 195
An abstract factory is a design pattern that solves the problem of creating entire product families without specifying their concrete classes.
The client needs to basically be able to create variants of products through 'abstract factories' that expose their creational methods.
I have provided a demo. It isnt exactly the same implementation as you may want, but it demonstrates the concepts of the design pattern.
First define the abstraction that each product family will implement:
public interface IAbstractFactory
{
public IElectricCar GetElectricCarModel();
public INonElectricCar GetNonElectricCarModel();
}
Then, create each family of a products.
IAudiFactory:
public class IAudiFactory : IAbstractFactory
{
public IElectricCar GetElectricCarModel()
{
return new AudiElectricCar();
}
public INonElectricCar GetNonElectricCarModel()
{
return new NonAudiElectricCar();
}
}
IBMWFactory:
public class IBMWFactory : IAbstractFactory
{
public IElectricCar GetElectricCarModel()
{
return new BMWElectricCar();
}
public INonElectricCar GetNonElectricCarModel()
{
return new NonBMWElectricCar();
}
}
Each family will enable the creation of a product variant.
Create an interface for each variant:
IElectricCar:
public interface IElectricCar
{
public CarModel GetDetails();
}
INonElectricCar:
public interface INonElectricCar
{
public CarModel GetDetails();
}
This is the car class that will be created:
public class CarModel
{
public string Year;
public string Make;
public string Model;
public string KiloMeters;
}
Create implementation of non electric and electric cars creators of each car factory type.
public class AudiElectricCar : IElectricCar
{
public CarModel GetDetails()
{
return new()
{
Year = "2021",
Make = "China",
Model = "Audi electric A1",
KiloMeters = "201"
};
}
}
public class NonAudiElectricCar : INonElectricCar
{
public CarModel GetDetails()
{
return new()
{
Year = "2020",
Make = "Rand1",
Model = "AUDI None electric A1",
KiloMeters = "20"
};
}
}
public class BMWElectricCar : IElectricCar
{
public CarModel GetDetails()
{
return new()
{
Year = "2021",
Make = "China",
Model = "BMW electric A1",
KiloMeters = "201"
};
}
}
public class NonBMWElectricCar : INonElectricCar
{
public CarModel GetDetails()
{
return new()
{
Year = "2020",
Make = "Rand1",
Model = "BMW None electric A1",
KiloMeters = "20"
};
}
}
Once you are done writing the abstraction and implementation of your design pattern, create the client logic.
class Client
{
INonElectricCar nonElectricCar;
IElectricCar electricCar;
public Client(IAbstractFactory factory)
{
electricCar = factory.GetElectricCarModel();
nonElectricCar = factory.GetNonElectricCarModel();
}
public CarModel GetElectricCarDetails()
{
return electricCar.GetDetails();
}
public CarModel GetNonElectricCarDetails()
{
return nonElectricCar.GetDetails();
}
}
The above logic doesnt have to be the same, as long the code runs successfully. An example of consuming the client in logic in action:
Client client = new Client(new IBMWFactory());
CarModel carModel = client.GetElectricCarDetails();
Console.WriteLine(carModel.Model);
CarModel carModel2 = client.GetNonElectricCarDetails();
Console.WriteLine(carModel2.Model);
Upvotes: 4