The Abstract Factory design pattern provides an interface for creating families of related objects without specifying their concrete classes.
Let us dive into a real world example: Electronics Store.
An electronic store has Mobile Phones, Laptops and Gaming Consoles. The manufacturers are Samsung, Sony and Microsoft having Mobile phones, laptops and gaming consoles.
Let us try to visualize this:

The program starts at:
1. ElectronicShowroom – This is a client which consumes abstract factory(IElectronics) and Abstract products(IMobilePhone, IGamingConsole, ILaptop)
2. IElectronics – An abstract factory implemented by concrete factories: Microsoft, Samsung, Sony
3. IMobilePhone – An abstract product implemented by mobile product family
4. IGamingConsole – An abstract product implemented by gaming console family
5. ILaptop – An abstract product implemented by laptop family.
The code is as below:
namespace DesignPatterns.AbstractFactory
{
public interface IElectronics
{
IMobilePhone GetMobilePhone();
ILaptop GetLaptop();
IGamingConsole GetGamingConsole();
}
public interface IMobilePhone
{
string GetMobilePhone();
}
public interface ILaptop
{
string GetLaptop();
}
public interface IGamingConsole
{
string GetGamingConsole();
}
public class Samsung : IElectronics
{
public IGamingConsole GetGamingConsole()
{
return new SamsungConsole();
}
public ILaptop GetLaptop()
{
return new SamsungLaptop();
}
public IMobilePhone GetMobilePhone()
{
return new SamsungGalaxy();
}
}
public class Microsoft : IElectronics
{
public IGamingConsole GetGamingConsole()
{
return new MicrosoftConsole();
}
public ILaptop GetLaptop()
{
return new MicrosoftLaptop();
}
public IMobilePhone GetMobilePhone()
{
return new MicrosoftPhone();
}
}
public class Sony : IElectronics
{
public IGamingConsole GetGamingConsole()
{
return new SonyConsole();
}
public ILaptop GetLaptop()
{
return new SonyLaptop();
}
public IMobilePhone GetMobilePhone()
{
return new SonyPhone();
}
}
public class SamsungGalaxy : IMobilePhone
{
public string GetMobilePhone()
{
return "Samsung Galaxy Phone";
}
}
public class SamsungZFlip : IMobilePhone
{
public string GetMobilePhone()
{
return "Samsung Z-Flip Phone";
}
}
public class SamsungLaptop : ILaptop
{
public string GetLaptop()
{
return "Samsung Laptop";
}
}
public class SamsungConsole : IGamingConsole
{
public string GetGamingConsole()
{
return "Samsung Gaming hub";
}
}
public class MicrosoftPhone : IMobilePhone
{
public string GetMobilePhone()
{
return "Microsoft Phone";
}
}
public class MicrosoftLaptop : ILaptop
{
public string GetLaptop()
{
return "Microsoft Laptop";
}
}
public class MicrosoftConsole : IGamingConsole
{
public string GetGamingConsole()
{
return "Microsoft XBOX";
}
}
public class SonyPhone : IMobilePhone
{
public string GetMobilePhone()
{
return "Sony Phone";
}
}
public class SonyLaptop : ILaptop
{
public string GetLaptop()
{
return "Sony ViAO";
}
}
public class SonyConsole : IGamingConsole
{
public string GetGamingConsole()
{
return "Sony Playstation 4";
}
}
public class ElectronicShowroomClient
{
IMobilePhone mobilePhone;
ILaptop laptop;
IGamingConsole gamingConsole;
public ElectronicShowroomClient(IElectronics electronics)
{
mobilePhone = electronics.GetMobilePhone();
laptop = electronics.GetLaptop();
gamingConsole = electronics.GetGamingConsole();
}
public string GetMobiles()
{
return mobilePhone.GetMobilePhone();
}
public string GetLatops()
{
return laptop.GetLaptop();
}
public string GetGamingConsoles()
{
return gamingConsole.GetGamingConsole();
}
}
}
The client code which is the starting point of the application will look like this:
public void OnGet()
{
IElectronics electronics = new Samsung();
ElectronicShowroomClient client = new ElectronicShowroomClient(electronics);
var samsungMobile = client.GetMobiles();
var samsungLaptop = client.GetLatops();
electronics = new DesignPatterns.AbstractFactory.Microsoft();
client = new ElectronicShowroomClient(electronics);
var microsoftMobile = client.GetMobiles();
var microsoftConsole = client.GetGamingConsoles();
electronics = new DesignPatterns.AbstractFactory.Sony();
client = new ElectronicShowroomClient(electronics);
var sonyLaptop = client.GetMobiles();
var sonyConsole = client.GetGamingConsoles();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("SamsungMobile =" + samsungMobile);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("SamsungLaptop =" + samsungLaptop);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("MicrosoftMobile =" + microsoftMobile);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("MicrosoftConsole =" + microsoftConsole);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("SonyLaptop =" + sonyLaptop);
stringBuilder.Append(Environment.NewLine);
stringBuilder.Append("SonyConsole =" + sonyConsole);
ViewData["Results"] = stringBuilder.ToString();
}
Results:
