Design pattern - abstract factory pattern

Keywords: Design Pattern

Design pattern - abstract factory pattern

meet Simple factory mode,Factory method model

In the factory method mode in the previous chapter, a factory only produces one product, which has good scalability, but in reality, factories are diversified. A factory can create a series of products. At this time, the factory method mode is not applicable. Therefore, the emergence of abstract factory solves the problem of creating a series of products.

Definition: abstract factory is used to create product families. Abstract Factory refers to a factory mode used when there are multiple Abstract roles. It provides customers with an interface to create products and can create products in a variety of product families.

Structure:
AbstractFactory (Abstract Factory)
It provides multiple interfaces for creating different products, and is not responsible for the creation of specific products. The abstract factory can be an abstract class or an interface

ConcreteFactory (specific factory)
Inherits AbstractFactory, implements the interfaces of all objects created by AbstractFactory, and is responsible for the creation of specific products.

Product (abstract, interface product)
All objects created by the factory pattern correspond to the parent class or interface of the class

Concrete Product
Target for factory creation

Similarly, taking the automobile factory as an example, an automobile factory can not only produce cars, but also produce a series of products such as tires and oil. At the same time, there are many different automobile companies in the world, and each automobile company has an independent factory.
Let's take BMW and Mercedes Benz as examples,
Demand BMW automobile factory and Mercedes Benz automobile factory to produce the company's own cars, tires and engine oil respectively

Abstract classes and interfaces of automobile, tire and engine oil

    // Automobile interface
    public interface ICar
    {
        void Print();
    }

    // Tire interface
    public interface ITyre
    {
        void Print();
    }

    // Oil interface
    public interface IEngineOil
    {
        void Print();
    }

Mercedes Benz products

    #region Benz products
    // Mercedes Benz car
    public class BenzSaloonCar : ICar
    {
        public void Print()
        {
            Console.WriteLine("This is a Mercedes Benz");
        }
    }

    // Mercedes Benz tire
    public class BenzTyre : ITyre
    {
        public void Print()
        {
            Console.WriteLine("This is a Mercedes Benz tire");
        }
    }

    // BENZ Oil
    public class BenzEngineOil : IEngineOil
    {
        public void Print()
        {
            Console.WriteLine("This is Benz oil");
        }
    }
    #endregion

BMW products

    #region BMW products
    // BMW car
    public class BMWSaloonCar : ICar
    {
        public void Print()
        {
            Console.WriteLine("This is a BMW car");
        }
    }

    // BMW tire
    public class BMWTyre : ITyre
    {
        public void Print()
        {
            Console.WriteLine("This is a BMW tire");
        }
    }

    // BMW oil
    public class BMWEngineOil : IEngineOil
    {
        public void Print()
        {
            Console.WriteLine("This is BMW oil");
        }
    }
    #endregion

Define factory abstract classes and interfaces. A factory can produce multiple products

    // Automotive factory interface
    public interface IAutomobileFactory
    {
        // Production vehicle interface
        ICar CreateSaloonCar();

        // Production tire interface
        ITyre CreateTyre();

        // Production oil interface
        IEngineOil CreateEngineOil();
    }

Specific factory:
Mercedes Benz automobile factory

    // Mercedes Benz automobile factory
    public class BenzAutomobileFactory : IAutomobileFactory
    {
        public ICar CreateSaloonCar()
        {
            ICar car = new BenzSaloonCar();
            return car;
        }

        public ITyre CreateTyre()
        {
            ITyre tyre = new BenzTyre();
            return tyre;
        }

        public IEngineOil CreateEngineOil()
        {
            IEngineOil engineOil = new BenzEngineOil();
            return engineOil;
        }
    }

BMW automobile factory

    // BMW automobile factory
    public class BMWAutomobileFactory : IAutomobileFactory
    {
        public ICar CreateSaloonCar()
        {
            ICar car = new BMWSaloonCar();
            return car;
        }

        public ITyre CreateTyre()
        {
            ITyre tyre = new BMWTyre();
            return tyre;
        }

        public IEngineOil CreateEngineOil()
        {
            IEngineOil engineOil = new BMWEngineOil();
            return engineOil;
        }
    }

The code call is as follows

    class Customer
    {
        public Customer()
        {
            // Create Mercedes Benz factory
            BenzAutomobileFactory benzAutomobileFactory = new BenzAutomobileFactory();

            // Production of Mercedes Benz cars
            ICar benzSaloonCar = benzAutomobileFactory.CreateSaloonCar();
            benzSaloonCar.Print();

            // Production of Mercedes Benz tires
            ITyre benzTyre = benzAutomobileFactory.CreateTyre();
            benzTyre.Print();

            // Production of BENZ Engine Oil
            IEngineOil benzEngineOil = benzAutomobileFactory.CreateEngineOil();
            benzEngineOil.Print();

            // Create BMW factory
            BMWAutomobileFactory bMWAutomobileFactory = new BMWAutomobileFactory();

            // Production of BMW cars
            ICar bmwSaloonCar = bMWAutomobileFactory.CreateSaloonCar();
            bmwSaloonCar.Print();

            // Production of BMW tires
            ITyre bmwTyre = bMWAutomobileFactory.CreateTyre();
            bmwTyre.Print();

            // Production of BMW oil
            IEngineOil bmwEngineOil = bMWAutomobileFactory.CreateEngineOil();
            bmwEngineOil.Print();

        }
    }

advantage:
(1) The generation of specific classes is isolated, so that customers do not need to care who is created and how to create, which improves the cohesion of classes and reduces the coupling between classes
(2) There is no need to modify the existing system to add a new specific factory

Disadvantages:
(1) It is difficult to expand. When adding a new product type, you need to modify the abstract factory class and interface, and then all specific factories need to add corresponding interfaces accordingly

Posted by alan543 on Mon, 01 Nov 2021 03:46:57 -0700