Abstract Factory :return families of related or dependent objects without specifying their concrete classes.
- AbstractFactory- IVehicle
- ConcreteFactory - Maruti, Honda
- AbstractProduct- IDiesel, IPetrol
- Product- DezirePetrol, ZenDiesel, AmazeDiesel, CityPetrol
- Client-This is a class which uses AbstractFactory and AbstractProduct interfaces to create a family of related objects
Implementation is pretty much straight forward: Since now you have good understanding of Factory, there is nothing much to explain.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFactory
{
class Program
{
static void Main(string[] args)
{
IVehicle marutiVehicle = new Maruti();
CarClient marutiClient = new CarClient(marutiVehicle);
Console.WriteLine("********* MARUTI **********");
Console.WriteLine(marutiClient.GetDieselModelDetails());
Console.WriteLine(marutiClient.GetPetrolModelDetails());
IVehicle hondaMobilePhone = new Honda();
CarClient hondaClient = new CarClient(hondaMobilePhone);
Console.WriteLine("******* HONDA **********");
Console.WriteLine(hondaClient.GetDieselModelDetails());
Console.WriteLine(hondaClient.GetPetrolModelDetails());
Console.ReadKey();
}
}
public interface IVehicle
{
IDieselVehicle GetDieselVehicle();
IPetrolVehicle GetPetrolVehicle();
}
public interface IPetrolVehicle
{
string GetModelDetails();
}
public interface IDieselVehicle
{
string GetModelDetails();
}
public class Maruti : IVehicle
{
public IDieselVehicle GetDieselVehicle()
{
return new DezirePetrol();
}
public IPetrolVehicle GetPetrolVehicle()
{
return new ZenDiesel();
}
}
public class ZenDiesel : IPetrolVehicle
{
public string GetModelDetails()
{
return "Maruti Zen Diesel Variant";
}
}
public class DezirePetrol : IDieselVehicle
{
public string GetModelDetails()
{
return "Maruti Swift Dezire Petrol Variant";
}
}
public class Honda : IVehicle
{
public IDieselVehicle GetDieselVehicle()
{
return new AmazeDiesel();
}
public IPetrolVehicle GetPetrolVehicle()
{
return new CityPetrol();
}
}
public class CityPetrol : IPetrolVehicle
{
public string GetModelDetails()
{
return "Honda City Petrol Variant";
}
}
public class AmazeDiesel : IDieselVehicle
{
public string GetModelDetails()
{
return "Amaze Diesel Variant";
}
}
public class CarClient
{
IPetrolVehicle petrolVehicle;
IDieselVehicle dieselVehicle;
public CarClient(IVehicle factory)
{
petrolVehicle = factory.GetPetrolVehicle();
dieselVehicle = factory.GetDieselVehicle();
}
public string GetPetrolModelDetails()
{
return petrolVehicle.GetModelDetails();
}
public string GetDieselModelDetails()
{
return dieselVehicle.GetModelDetails();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AbstractFactory
{
class Program
{
static void Main(string[] args)
{
IVehicle marutiVehicle = new Maruti();
CarClient marutiClient = new CarClient(marutiVehicle);
Console.WriteLine("********* MARUTI **********");
Console.WriteLine(marutiClient.GetDieselModelDetails());
Console.WriteLine(marutiClient.GetPetrolModelDetails());
IVehicle hondaMobilePhone = new Honda();
CarClient hondaClient = new CarClient(hondaMobilePhone);
Console.WriteLine("******* HONDA **********");
Console.WriteLine(hondaClient.GetDieselModelDetails());
Console.WriteLine(hondaClient.GetPetrolModelDetails());
Console.ReadKey();
}
}
public interface IVehicle
{
IDieselVehicle GetDieselVehicle();
IPetrolVehicle GetPetrolVehicle();
}
public interface IPetrolVehicle
{
string GetModelDetails();
}
public interface IDieselVehicle
{
string GetModelDetails();
}
public class Maruti : IVehicle
{
public IDieselVehicle GetDieselVehicle()
{
return new DezirePetrol();
}
public IPetrolVehicle GetPetrolVehicle()
{
return new ZenDiesel();
}
}
public class ZenDiesel : IPetrolVehicle
{
public string GetModelDetails()
{
return "Maruti Zen Diesel Variant";
}
}
public class DezirePetrol : IDieselVehicle
{
public string GetModelDetails()
{
return "Maruti Swift Dezire Petrol Variant";
}
}
public class Honda : IVehicle
{
public IDieselVehicle GetDieselVehicle()
{
return new AmazeDiesel();
}
public IPetrolVehicle GetPetrolVehicle()
{
return new CityPetrol();
}
}
public class CityPetrol : IPetrolVehicle
{
public string GetModelDetails()
{
return "Honda City Petrol Variant";
}
}
public class AmazeDiesel : IDieselVehicle
{
public string GetModelDetails()
{
return "Amaze Diesel Variant";
}
}
public class CarClient
{
IPetrolVehicle petrolVehicle;
IDieselVehicle dieselVehicle;
public CarClient(IVehicle factory)
{
petrolVehicle = factory.GetPetrolVehicle();
dieselVehicle = factory.GetDieselVehicle();
}
public string GetPetrolModelDetails()
{
return petrolVehicle.GetModelDetails();
}
public string GetDieselModelDetails()
{
return dieselVehicle.GetModelDetails();
}
}
}
Comments
Post a Comment