Abstract Factory Pattern

Abstract Factory Pattern

1. Introduction

The abstract factory pattern is actually an upgraded version of the simple factory pattern. In contrast, it follows the open-closed principle, unlike a simple factory, when we add a new class, we don't need to modify the factory class like the original simple factory.

2. Simple Demo

Let's take the example of a simple factory as an example. It turns out that our red cars and blue cars. If we want to add a black car, then we also need to add a class:

        /// <summary>
        /// 黒车
        /// </summary>
        public class BlackCar : Car
        {
            public override void run()
            {
                Console.WriteLine("I'm a BlackCar,I'm running!");
            }
        }

Then modify the car factory class:

        ///  <summary> 
        /// Car Factory
         ///  </summary> 
        public  class CarFactory
        {
            ///  <summary> 
            /// According to the color of the car to get the specific car
             ///  </summary> 
            ///  <param name="carColor"></param> 
            ///  <returns></returns> 
            public  static Car GetCar( string carColor )
            {
                Car ResultItem = null;
                switch (carColor)
                {
                    case "red":
                        ResultItem = new RedCar();
                        break;
                    case "blue":
                        ResultItem = new BlueCar();
                        break;
                    case "black":
                        ResultItem = new BlackCar();//Increase the judgment of black car
                         break ;
                     default :
                        ResultItem = new DefaultCar();
                        break;
                }
                return ResultItem;
            }
        }

In this way, have we also completed the function of adding black cars?

However, we violated the six principles of design patterns: the open-closed principle.

Now we have introduced the abstract factory, let the abstract factory help us solve this problem, let the specific factory produce cars, and let the client decide what car to produce, to improve our code:

First, write an abstract car factory class 

        ///  <summary> 
        /// Abstract car factory class
         ///  </summary> 
        public  abstract  class CarProductFactory
        {
            ///  <summary> 
            /// Abstract Factory Method
             ///  </summary> 
            ///  <returns></returns> 
            public  abstract Car CreateCar();
        }

For the corresponding car, complete the corresponding car factory:

        ///  <summary> 
        /// BlueCar Factory
         ///  </summary> 
        public  class BlueCarFactory : CarProductFactory
        {
            /// <summary>
            /// 生产车
            /// </summary>
            /// <returns></returns>
            public override Car CreateCar()
            {
                return new BlueCar();
            }
        }


        ///  <summary> 
        /// Red Car Factory
         ///  </summary> 
        public  class RedCarFactory : CarProductFactory
        {
            public override Car CreateCar()
            {
                return new RedCar();
            }
        }


        ///  <summary> 
        /// Black Car Factory
         ///  </summary> 
        public  class BlackCarFactory : CarProductFactory
        {
            public override Car CreateCar()
            {
                return new BlackCar();
            }
        }

Next, let's change the place where the call is made:

        static void Main(string[] args)
        {

            CarProductFactory blueCarFactory = new BlueCarFactory();
            Car blueCar= blueCarFactory.CreateCar();
            blueCar.run();


            CarProductFactory redCarFactory = new RedCarFactory();
            Car redCar= redCarFactory.CreateCar();
            redCar.run();


            CarProductFactory blackCarFactory = new BlackCarFactory();
            Car blackCar = blackCarFactory.CreateCar();
            blackCar.run();

            Console.Read();
        }    

In this way, our modification is completed.

3. Summary

Briefly talk about the idea (based on the simple factory modification we have done):

1. Create a factory class that implements an abstract implementation that generates the actual factory

2. Implement the factory class of specific generated classes (that is, our red car factory class, blue car factory class, and black car factory class)

3. Hand over the judgment logic in the original simple factory generation class to the client, and let the client decide which factory class to call to generate the corresponding data.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324769821&siteId=291194637