Factory pattern in ASP.NET

I. what is the factory mode?

1. Factory pattern is to define an interface for creating objects and let subclasses decide which class to instantiate.

2. Initialize encapsulation. That is to say, when you need an instance of a class, if you need an instance with the same attribute, the initialization operation is the same every time. You can encapsulate the initialization with the factory mode to simplify the operation.

Factory model and the simple factory model in the previous article( https://blog.csdn.net/Jie_Studio/article/details/88963624 )Almost the same as upgrading a simple factory, let's use a small case to show:

1. First, define an abstract class of food:

    /// <summary>

    ///Abstract class of food

    /// </summary>

    public abstract class Food

    {

        // Output what you bought online

        public abstract void Print();

    }

2. Define the specific food categories to be realized:

    /// <summary>

    //Milk type

    /// </summary>

    public class Milk : Food

    {

 

        public override void Print()

        {

            Console.WriteLine("A bottle of milk!");

        }

 

    }

 

    /// <summary>

    //Spicy strips

    /// </summary>

    public class Spicystrips : Food

    {

 

        public override void Print()

        {

            Console.WriteLine("A packet of spicy strips");

        }

 

    }

3. Define a retail factory class:

    /// <summary>
    ///Retail factory (factory mode)
    /// </summary>
    public abstract class FoodSimpleFactory
    {

        public abstract Food CreateFacory();

    }

4. Defined category factory:

    /// <summary>
    ///Beverage factory
    /// </summary>
    public class DrinkFactory : FoodSimpleFactory
    {

        public override Food CreateFacory()
        {

            return new Milk();

        }

    }

    /// <summary>
    ///Snack factory
    /// </summary>
    public class SnacksFactoyr : FoodSimpleFactory
    {

        public override Food CreateFacory()
        {

            return new Spicystrips();

        }

    }

5. Finally, call through the client:

    class Program
    {
        static void Main(string[] args)
        {

            //Initialize factory - Beverage
            FoodSimpleFactory foodfactory = new DrinkFactory();

            //milk
            Food food = foodfactory.CreateFacory();
            food.Print();

            Console.Read();

        }
    }

6. If you want to change to snacks, just change:

//Initialization factory -- snack class

FoodSimpleFactory foodfactory = new SnacksFactoyr();

Use scenarios of factory mode:

1. The client does not need to know the class of the object it creates. In the example, we don't know the specific name of each image loader, only the name of the factory that created it completes the creation process.

2. The client can specify to create corresponding objects through subclasses.

Tags: Attribute

Posted on Sat, 30 Nov 2019 18:07:18 -0800 by denniston