Tutorials Design Patterns in C#
Abstract Factory Pattern — Complete Guide
Abstract Factory Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.
On this page
Design Patterns in C# · Lesson 3 of 69
Abstract Factory Pattern
GoF Core → Enterprise → Cloud & Craft
GoF Core · 1 — Create & structure · ~6 min · Module 1: Creational Design Patterns
What is this?
Abstract Factory builds families of related objects (e.g. UI or payment kits) without binding to concrete classes.
Why should you care?
ShopNest “Domestic” vs “International” checkout needs matching tax + shipping calculators as one family.
See it live — copy this example
Paste into a C# console or class library project and run dotnet run.
public interface ITaxCalc { decimal Tax(decimal subtotal); }
public interface IShippingCalc { decimal Ship(decimal weightKg); }
public interface ICheckoutFactory
{
ITaxCalc Tax();
IShippingCalc Shipping();
}
public sealed class IndiaCheckoutFactory : ICheckoutFactory
{
public ITaxCalc Tax() => new GstTax();
public IShippingCalc Shipping() => new IndiaShipping();
}
public sealed class GstTax : ITaxCalc
{
public decimal Tax(decimal s) => Math.Round(s * 0.18m, 2);
}
public sealed class IndiaShipping : IShippingCalc
{
public decimal Ship(decimal w) => 40 + w * 10;
}
ICheckoutFactory f = new IndiaCheckoutFactory();
Console.WriteLine(f.Tax().Tax(1000) + f.Shipping().Ship(2));
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- IndiaCheckoutFactory returns a consistent pair.
- Swap to an EU factory and both policies change together — that is the family guarantee.
Practice next
- Run India totals for subtotal 1000.
- Add EuCheckoutFactory with different rates.
- Inject ICheckoutFactory based on shipping country.
- Add IInvoiceFormatter to the family.
- Select factory from country code.
Remember
Families of products together. One factory per variant. Callers use interfaces only.
ShopNest regional checkout kit
Country selects a checkout factory.
Outcome: Tax and shipping rules stay consistent per region.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!