C# Factory Design Patterns

Explore how the C# Factory Design Pattern simplifies object creation and supports runtime flexibility. See a real-world example using an app theme switcher (light/dark mode) and learn its architectural benefits.

C# Factory Design Patterns
Photo by Lalit Kumar / Unsplash

The Factory Pattern is a creational design pattern that delegates the responsibility of object instantiation to a factory method or class. It helps you encapsulate object creation, making your codebase flexible, testable, and easy to maintain.


Implementing

Let’s walk through your implementation of a light/dark mode switcher using the Factory Pattern.

🔸 Theme Interface

internal interface IAppTheme
{
        string TextColor { get; }
        string BgrColor { get; }
}

This defines a contract for any theme. Both light and dark modes implement this.


🔸 Light and Dark Theme Classes

internal class LightTheme : IAppTheme
{
        public string TextColor => "black";
        public string BgrColor => "white";
}

internal class DarkTheme : IAppTheme
{
        public string TextColor => "white";
        public string BgrColor => "dark gray";
}

These classes are concrete implementations of the interface. One is optimized for bright environments; the other for dark ones.


🔸Replaceable Factory

    internal class AppThemeFactory
    {
        private readonly List<WeakReference<ThemeRef>> _themes = new();

        private IAppTheme CreateThemeImpl(bool dark) => dark ? new DarkTheme() : new LightTheme();

        public IAppTheme CreateTheme(bool dark)
        {
            var themeRef = new ThemeRef(CreateThemeImpl(dark));
            _themes.Add(new WeakReference<ThemeRef>(themeRef));
            return themeRef;
        }

        // Swap all themes at once (e.g., toggle dark mode)  
        public void ReplaceTheme(bool dark)
        {
            foreach (var wr in _themes)
            {
                if (wr.TryGetTarget(out var reference))
                    reference.Value = CreateThemeImpl(dark);
            }
        }

        private class ThemeRef : Ref<IAppTheme>, IAppTheme
        {
            public ThemeRef(IAppTheme value) : base(value) { }
            public string TextColor => Value.TextColor;
            public string BgrColor => Value.BgrColor;
        }
    }
    internal class Ref<T> where T : class
    {
        public T Value;
        public Ref(T value) => Value = value;
    }

Switch themes globally in real-time — like toggling dark mode across your entire app.


Usage Example

var factory = new AppThemeFactory();
var adminTheme = factory.CreateTheme(true);
Console.WriteLine(adminTheme.BgrColor); // "dark gray"  

// Switch all themes to light mode  
factory.ReplaceTheme(false);
Console.WriteLine(adminTheme.BgrColor); // "white"  

🧠 Final Thoughts

The C# Factory Pattern is an elegant solution for object creation where flexibility and abstraction are essential. By using this pattern, you future-proof your code, simplify testing, and create scalable features — like our app theme switcher.

Subscribe to arash.ninja

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe