Design pattern - Static Factory Method

This blog is written with reference to the boss Learning hard and his own personal understanding. There may be something wrong with the content. I hope you guys don't hesitate to give me advice

Portal: https://www.cnblogs.com/zhili/p/DesignPatternSummery.html

Simple Factory Pattern is an innovative pattern of classes, also known as static factory method pattern. It is responsible for creating instances of other classes by specifically defining a class. The created instances usually have a common parent class.

Advantages: the specified class can be generated dynamically through the conditions passed in by the user

Disadvantages: if the factory has problems, all users will be paralyzed

The roles included in the simple factory model and their corresponding responsibilities are as follows:

  • Creator: This is the core of the simple factory pattern, which is responsible for creating the internal logic of all classes. Of course, the factory class must be able to be called by the outside world to create the required product object.
  • Abstract Product role: the parent class of all objects created by the simple factory pattern. Note that the parent class here can be an interface or an abstract class, which is responsible for describing the common interface shared by all instances.
  • Concrete Product role: a concrete instance object created by a simple factory. These concrete products often have a common parent class.

In depth understanding

  • Specifically, the product is regarded as a collection of a series of classes, which are an object tree derived from an abstract class or interface. The factory class is used to generate an appropriate object to meet customer requirements.

  • If there is no common logic between the specific products involved in the simple factory pattern, we can use interfaces to play the role of abstract products; If there is functional logic or between specific products, we must extract these common things and put them in an abstract class, and then let the specific products inherit the abstract class. For the purpose of better reuse, common things should always be abstracted.

The following statement is a condition. We need two kinds of food. At this time, we only need to state which food we need. We just need to say it. The specific inheritance of food is the abstract food class. The food factory produces and returns the food we ordered. The specific code is as follows

Click to view the code
    internal class Program
    {
        static void Main(string[] args)
        {
            FoodClass food1 = CreateFoodEasyFactory.ReturnWhichFood("Food1");
            food1.foodPrice = "10.5";
            food1.FoodPrintfFunction();

            FoodClass food2 = CreateFoodEasyFactory.ReturnWhichFood("Food2");
            food2.foodPrice = "20.8";
            food2.FoodPrintfFunction();

            try
            {
                float cost = float.Parse(food1.foodPrice) + float.Parse(food2.foodPrice);
                Console.WriteLine(cost.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
        /// <summary>
        ///Declare the Food class. What properties and methods does this Food have
        /// </summary>
        public abstract class FoodClass
        {
            public abstract string foodPrice { get; set; }
            public abstract void FoodPrintfFunction();
        }
        /// <summary>
        ///Food1 inherits the FoodClass class
        /// </summary>
        public class Food1 : FoodClass
        {
            public override string foodPrice { get; set; }

            public override void FoodPrintfFunction()
            {
                Console.WriteLine(string.Format("You order food1,Pay for{0}", foodPrice));
            }
        }
        /// <summary>
        ///Food2 inherits FoodClass
        /// </summary>
        public class Food2 : FoodClass
        {
            public override string foodPrice { get; set; }

            public override void FoodPrintfFunction()
            {
                Console.WriteLine(string.Format("You order food2,Pay for{0}", foodPrice));
            }
        }
        /// <summary>
        ///Declare the Food production factory. The factory judges what classes are generated and returned
        /// </summary>
        public static class CreateFoodEasyFactory
        {
            public static FoodClass ReturnWhichFood(string foodName)
            {
                FoodClass fc = null;
                switch (foodName)
                {
                    case "Food1":fc = new Food1();break;
                    case "Food2":fc =  new Food2(); break;
                    default:fc = null;break;
                }
                return fc;
            }
        }
    }

Keywords: Design Pattern DotNet

Added by Attila on Tue, 11 Jan 2022 11:11:27 +0200