Tutorials Design Patterns Mastery

Abstract Factory: Creating families of related objects

On this page

Abstract Factory Pattern

The Abstract Factory is a "Factory of Factories." It provides an interface for creating families of related or dependent objects without specifying their concrete classes. This is the ultimate tool for building Cross-Platform or Multi-Themed applications.

1. Scenario: Multi-Cloud Storage

Imagine your app supports both AWS and Azure. Each cloud has its own Storage and its own Network logic. You don't want to mix AWS logic with Azure objects.

public interface ICloudFactory 
{
    IStorage CreateStorage();
    INetwork CreateNetwork();
}

// Ensure we ONLY create AWS objects together
public class AwsFactory : ICloudFactory 
{
    public IStorage CreateStorage() => new S3Bucket();
    public INetwork CreateNetwork() => new AwsVpc();
}

2. Difference between Factory vs Abstract Factory

  • Factory Method: Creates ONE product (e.g., A Logger).
  • Abstract Factory: Creates a FAMILY of products (e.g., A SQL Driver AND a SQL Connection AND a SQL Command).

4. Interview Mastery

Q: "How does the Abstract Factory pattern relate to the Open/Closed Principle?"

Architect Answer: "It follows OCP perfectly. If we want to add support for a third cloud (e.g., Google Cloud), we don't change any existing code. We simply create a new `GoogleCloudFactory` class and implement the interfaces. The main application logic remains 100% untouched because it only talks to the top-level `ICloudFactory` interface."

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 Mastery
Course syllabus
1. Introduction to Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Modern Enterprise & Cloud Patterns
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