Abstract Factory - Design Pattern

Abstract Factory - Design Pattern

Software Architecture Simplified

ยท

3 min read

Objective ๐ŸŽฏ

Allow the creation of families of related or dependents objects without specifying their concrete classes.

Type โœ…

โŒBehavioral: Describes how objects interact/communicate between themselves.

โœ”๏ธCreational: Describes how to instantiate an object without large and complex.

โŒStructural: Describes how objects/classes are composed to form larger structures.

UML ๐Ÿ“

image.png

Participants ๐Ÿ”—

โ€ข AbstractFactory:

  • Declares an interface with methods that create abstract products

โ€ข ConcreteFactory:

  • Implements the necessary operations to create product objects

โ€ข AbstractProduct:

  • Declares an interface for a specific type of product

โ€ข Product:

  • Implements the AbstractProduct interface

โ€ข Client:

  • Uses a concrete implementation of AbstractFactory to create concrete implementations of AbstractProducts

Sample Code ๐ŸŽฎ

Structural Example ๐Ÿ›๏ธ

image.png

public static class AbstractFactoryStructural
    {
        public static void Execute()
        {
            AbstractFactory lAbstractFactory = new ConcreteFactory1();
            Client lClient = new Client(lAbstractFactory);
            lClient.Execute();

            lAbstractFactory = new ConcreteFactory2();
            lClient = new Client(lAbstractFactory);
            lClient.Execute();
        }
    }

    public abstract class AbstractProductA
    {
    }

    public abstract class AbstractProductB
    {
        public abstract void Interact(AbstractProductA prAbstractProductA);
    }

    public class ProductA1 : AbstractProductA
    {
    }

    public class ProductA2 : AbstractProductA
    {
    }

    public class ProductB1 : AbstractProductB
    {
        public override void Interact(AbstractProductA prAbstractProductA)
        {
            Console.WriteLine(this.GetType().Name + " is interacting with " + prAbstractProductA.GetType().Name);
        }
    }

    public class ProductB2 : AbstractProductB
    {
        public override void Interact(AbstractProductA prAbstractProductA)
        {
            Console.WriteLine(this.GetType().Name + " is interacting with " + prAbstractProductA.GetType().Name);
        }
    }

    abstract public class AbstractFactory
    {
        public abstract AbstractProductA CreateProductA();
        public abstract AbstractProductB CreateProductB();
    }

    public class ConcreteFactory1 : AbstractFactory
    {
        public override ProductA1 CreateProductA()
        {
            return new ProductA1();
        }
        public override ProductB1 CreateProductB()
        {
            return new ProductB1();
        }
    }

    public class ConcreteFactory2 : AbstractFactory
    {
        public override ProductA2 CreateProductA()
        {
            return new ProductA2();
        }
        public override ProductB2 CreateProductB()
        {
            return new ProductB2();
        }
    }

    public class Client
    {
        private AbstractFactory _AbstractFactory;
        private AbstractProductA _AbstractProductA;
        private AbstractProductB _AbstractProductB;

        public Client(AbstractFactory prAbstractFactory)
        {
            _AbstractFactory = prAbstractFactory;
        }

        public void Execute()
        {
            _AbstractProductA = _AbstractFactory.CreateProductA();
            Console.WriteLine(_AbstractFactory.GetType().Name + " created Product " + _AbstractProductA.GetType().Name);
            _AbstractProductB = _AbstractFactory.CreateProductB();
            Console.WriteLine(_AbstractFactory.GetType().Name + " created Product " + _AbstractProductB.GetType().Name);
            _AbstractProductB.Interact(_AbstractProductA);
        }
    }

Output

image.png

Real-world Example ๐Ÿ”ฅ

image.png

public static class AbstractFactoryPractical
    {
        public static void Execute()
        {
            LaptopProducer lLaptopProducer = new LaptopProducer(new DellFactory());
            lLaptopProducer.ProduceLaptopParts();

            lLaptopProducer = new LaptopProducer(new AppleFactory());
            lLaptopProducer.ProduceLaptopParts();
        }
    }

    public abstract class Processor { }
    public abstract class Motherboard { }
    public abstract class Storage { }
    public class DellProcessor : Processor { }
    public class AppleProcessor : Processor { }
    public class DellMotherboard : Motherboard { }
    public class AppleMotherboard : Motherboard { }
    public class DellStorage : Storage { }
    public class AppleStorage : Storage { }

    abstract public class LaptopFactory
    {
        public abstract Processor CreateProcessor();
        public abstract Motherboard CreateMotherboard();
        public abstract Storage CreateStorage();
    }

    public class DellFactory : LaptopFactory
    {
        public override Processor CreateProcessor()
        {
            Console.WriteLine(this.GetType().Name + " - Processor created");
            return new DellProcessor();
        }
        public override Motherboard CreateMotherboard()
        {
            Console.WriteLine(this.GetType().Name + " - Motherboard created");
            return new DellMotherboard();
        }
        public override Storage CreateStorage()
        {
            Console.WriteLine(this.GetType().Name + " - Storage created");
            return new DellStorage();
        }
    }

    public class AppleFactory : LaptopFactory
    {
        public override Processor CreateProcessor()
        {
            Console.WriteLine(this.GetType().Name + " - Processor created");
            return new DellProcessor();
        }
        public override Motherboard CreateMotherboard()
        {
            Console.WriteLine(this.GetType().Name + " - Motherboard created");
            return new DellMotherboard();
        }
        public override Storage CreateStorage()
        {
            Console.WriteLine(this.GetType().Name + " - Storage created");
            return new DellStorage();
        }
    }

    public class LaptopProducer
    {
        private LaptopFactory _LaptopFactory;
        private Processor _Processor;
        private Motherboard _Motherboard;
        private Storage _Storage;

        public LaptopProducer(LaptopFactory prLaptopFactory)
        {
            _LaptopFactory = prLaptopFactory;
        }

        public void ProduceLaptopParts()
        {
            _Processor = _LaptopFactory.CreateProcessor();
            _Motherboard = _LaptopFactory.CreateMotherboard();
            _Storage = _LaptopFactory.CreateStorage();
        }
    }

Output

image.png

Source Code ๐ŸŽฒ

github.com/VictorLins/DesignPatterns

Did you find this article valuable?

Support Victor Lins by becoming a sponsor. Any amount is appreciated!

ย