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 CoreEnterpriseCloud & 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.

Code
Result

What happened?

  • IndiaCheckoutFactory returns a consistent pair.
  • Swap to an EU factory and both policies change together — that is the family guarantee.

Practice next

  1. Run India totals for subtotal 1000.
  2. Add EuCheckoutFactory with different rates.
  3. Inject ICheckoutFactory based on shipping country.
  4. Add IInvoiceFormatter to the family.
  5. 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.

Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Abstract Factory Interface:?
Short answer: The IUIFactory interface defines methods for creating abstract product types like buttons and checkboxes. This ensures that every concrete factory will implement the same methods, but each will provide plat…
Mid PDF Detailed
Abstract Expression (IExpression):?
Short answer: The IExpression interface declares an Interpret method, which is the core of the interpreter pattern. This method is used to interpret and evaluate expressions. public interface IExpression Example code { i…
Mid PDF Detailed
Dynamic Factory Selection:?
Short answer: In a more advanced system, you could dynamically choose which factory to use based on external configurations, like settings or environment variables. This would enable the system to switch between differen…
Mid PDF Detailed
Abstraction:?
Short answer: The Shape class is the abstraction. It holds a reference to the IDrawingAPI and delegates the drawing task to it. It defines the common interface Draw() that all shapes must implement. Example code public a…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details