C# Design Patterns - Abstract Factory Pattern

1. Introduction to Abstract Factory Pattern

  In the previous article, we learned about the factory pattern and know that the factory pattern can solve the defects of the simple factory (the factory class needs to be modified when adding a new product in the simple factory, which does not conform to the open-closed principle), but both the simple factory and the factory pattern only produce one kind of product (The simple factories and factories in the front only produce mice.) In fact, Dell and HP not only produce mice but also keyboards. In order to solve the problem of series products, there is an abstract factory model. Let's take the production of mouse and keyboard as an example to understand the abstract factory.

Product categories, including mouse and keyboard categories:

   //mouse abstract class
    public abstract class Mouse
    {
        public abstract void Print();
    }

   //dell mouse
    public class DellMouse : Mouse
    {
        public override void Print()
        {
            Console.WriteLine("produced a Dell mouse!");
        }
    }

    //HP mouse
    public class HpMouse : Mouse
    {
        public override void Print()
        {
            Console.WriteLine("An HP mouse was produced!");
        }
    }


   //keyboard abstract class
    public abstract class Keybo
    {
        public abstract void Print();
    }

    //Dell keyboard class
    public class DellKeybo : Keybo
    {
        public override void Print()
        {
            Console.WriteLine("A Dell keyboard was produced!");
        }
    }

   //HP keyboard
    public class HpKeybo : Keybo
    {
        public override void Print()
        {
            Console.WriteLine("An HP keyboard was produced!");
        }
    }

Factory class, the abstract factory class provides abstract methods for producing mouse and keyboard. Dell factory inherits the abstract factory, and only creates its own brand of mouse and keyboard when implementing the abstract method; the same is true for the HP factory, the code is as follows:

    /// <summary>
    ///Pc Product Factory abstract class
    /// </summary>
    public abstract class PcFactory
    {
        public abstract Mouse CreateMouse();
        public abstract Keybo CreateKeybo();
    }

    //dell pc factory
    public class DellPcFactroy : PcFactory
    {
        public override Keybo CreateKeybo()
        {
            return new DellKeybo();
        }

        public override Mouse CreateMouse()
        {
            return new DellMouse();
        }
    }

    //HP pc factory
    public class HpPcFactory : PcFactory
    {
        public override Mouse CreateMouse()
        {
            return new HpMouse();
        }
        public override Keybo CreateKeybo()
        {
            return new HpKeybo();
        }

    }

Client code:

   class Program
    {
        static void Main(string[] args)
        {
            //Producing a Dell Mouse/Keyboard
            PcFactory dellFactory = new DellPcFactroy();
            Mouse dellMouse= dellFactory.CreateMouse();
            Keybo dellKeybo = dellFactory.CreateKeybo();
            dellMouse.Print();
            dellKeybo.Print();

            //Produce an HP mouse/keyboard
            PcFactory hpFactory = new HpPcFactory();
            Mouse hpMouse = hpFactory.CreateMouse();
            Keybo hpKeybo = hpFactory.CreateKeybo();
            hpMouse.Print();
            hpKeybo.Print();
            Console.ReadKey();
        }
    }

The result of running the program is as follows:

We can see that the difference between the abstract factory and the factory pattern is that the abstract factory can produce multiple products (two types: mouse and keyboard), while the factory pattern only produces one product (keyboard), and the abstract factory mainly solves the problem of producing a series of products. question. Think about it: what if there was only one product in the abstract factory? An abstract factory with only one product is actually a factory pattern, and we can think of the factory pattern as a special case of an abstract factory.

back to the top

2. Summary

The class diagram of the above example:

 

Advantages of abstract factories: Abstract factories have the advantages of the factory pattern, which conforms to the closure principle for adding series products (there is only one series of products in the factory pattern). If you want to produce ASUS mouse and keyboard, you only need to add an AsusPcFactory (inherited from PcFactory), AsusMouse (inherited from Mouse), AsusKeybo (inherited from Keybo), and you can produce ASUS mouse and keyboard on the client through the following code:

PcFactory asusPcFactory=new AsusPcFactory();
asusPcFactory.CreateMouse();//production asus mouse
asusPcFactory.CreaetKeybo();//Production of Asus keyboards

Disadvantages of Abstract Patterns: Abstract patterns are not open-closed for adding new products. If you want to produce a display, you not only need to add the display abstract class and the display concrete class, but also need to modify the PcFactory, add a CreateDisplay abstract method (or interface) in the abstract factory, and the Dell factory and the HP factory also need to implement the new abstract method (or interface).

Posted by vboyz on Sun, 08 May 2022 19:56:24 +0300