[design mode] simple factory

Time:2021-8-26

Factory mode

Factory mode: as the name suggests, this class is used to produce products like factories. We are the production class. Factory mode is the mode of creating production classes.

It provides a method to create an object in the parent class, allowing the subclass to determine the type of instantiated object.

Code binding to specific classes will make the code more fragile, less elastic, difficult to expand and difficult to modify.

For interface programming, it can isolate a lot of changes that may occur in the system in the future and is easy to expand.

It is used to deal with change and help us “find out the aspects that will change and separate them from the unchanged parts”

Demo

[design mode] simple factory
For example, there is a logistics company that used to have a single business and only engaged in land logistics. With the passage of time and the change of the market, there is also marine logistics. How to design an implementation method to deal with this uncertainty in business logic? If an air logistics is added later, how will it be designed.

How to only have road logistics and marine logistics? We can only declare them in separate classes, and then judge the logistics through if / eles, so as to realize it. This is no problem and can solve the problems faced by the business, but it is not the best implementation method in software development. If you implement it through if / eles, their coupling is too high. In the later stage, if the land logistics is modified, there will be too many modifications and it is not easy to expand.

Simple implementation logic

/// <summary>
    ///Marine logistics
    /// </summary>
    public class AtSeaLogistics
    {
        /// <summary>
        ///Shipping by sea
        /// </summary>
        public void SendSeaGoods(string goodName) 
        {
            Console. Writeline ("send:" + goodname);
        }
    }
/// <summary>
    ///Land logistics
    /// </summary>
    public class LandLogistics
    {
        /// <summary>
        ///Land shipment
        /// </summary>
        public void SendLandGoods(string goodName)
        {
            Console. Writeline ("land send:" + goodname);
        }
    }
static void Main(string[] args)
        {
            int logisticsType = 0;                                      // The default is land transportation
            Console.writeline ("start shipping");
            if (logisticsType==0)
            {
                LandLogistics land = new LandLogistics();
                land.SendLandGoods("Iphone 13");
            }
            else
            {
                AtSeaLogistics atSea = new AtSeaLogistics();
                Atsea. Sendseagoods ("sea fish");
            }
            Console.writeline ("shipment completed");
            Console.ReadKey();
        }

After running, since it is the default land shipment, the land shipment module will be called for shipment.
[design mode] simple factory
In fact, this is a very simple and the most commonly used way for us to write simple logic. There is no problem in realizing the function, but this implementation method has great hidden dangers and is not extensible. If a transportation method needs to be added later, it may need to be modified in the main () method. The coupling between transportation method and logistics is too high and it is not easy to expand.

The following is the implementation of using the simple factory mode. Each is implemented through the interface and is responsible for their own affairs. The logistics company does not need to know which method to use for delivery, but only needs to deliver the goods to the delivery factory. The delivery factory decides which method to use for delivery according to the goods itself.

Factory mode

    public interface ISendInterface
    {
        void SendGoods(string goodName);
    }
/// <summary>
    ///Land logistics
    /// </summary>
    public class LandLogistics:ISendInterface
    {
        /// <summary>
        ///Land shipment
        /// </summary>
        public void SendGoods(string goodName)
        {
            Console. Writeline ("land send:" + goodname);            
        }

        ///// <summary>
        /////Land shipment
        ///// </summary>
        //public void SendLandGoods(string goodName)
        //{
        //Console. Writeline ("land send:" + goodname);
        //}
    }
/// <summary>
    ///Marine logistics
    /// </summary>
    public class AtSeaLogistics:ISendInterface
    {
         /// <summary>
        ///Shipping by sea
        /// </summary>
        public void SendGoods(string goodName)
        {
            Console. Writeline ("send:" + goodname);            
        }

        //public void SendSeaGoods(string goodName) 
        //{
        //Console. Writeline ("send:" + goodname);
        //}
    }
/// <summary>
    ///Logistics management, mainly used for delivery
    /// </summary>
    public class LogisticsStoreFactory
    {
        ISendInterface sendGood = null;
        public ISendInterface GetSendInterface(int type)
        {
            switch (type)
            {
                case 1:
                    sendGood = new LandLogistics();
                    break;
                case 2:
                    sendGood = new AtSeaLogistics();
                    break;
                default:
                    break;
            }
            return sendGood;
        }
    }

Call implementation logic

LogisticsStoreFactory logisticsStore = new LogisticsStoreFactory();
            //Land transportation
            ISendInterface loadSend = logisticsStore.GetSendInterface(1);
            loadSend.SendGoods("Iphone 13");

            //Maritime Transportation

            ISendInterface atSeaSend = logisticsStore.GetSendInterface(2);
            Atseasend. Sendgoods ("sea fish");

            Console.writeline ("shipment completed");
            Console.ReadKey();

Now we can basically see that in the implementation process of simple factories, we don’t need to pay attention to how to realize their own transportation. We just get the implementation through the logisticstorefactory factory class and use it immediately. In this way, decoupling is realized, and the call and implementation are split. You don’t need to pay too much attention to how it is implemented, but only use it.

Applicable scenario

  • Simple factory can be used when object categories and their specific dependencies cannot be predicted during coding.
  • The creation and implementation are separated. When other products are not affected, they can be created independently, with low coupling and easy expansion.

    Implementation mode

  • Define each product class and let each class do their own work without interference with each other.
  • Specify a common interface first, so that all products can implement this interface.
  • Create an empty factory class, let it have the ability to obtain products, and return this common interface.
  • Find the reference to the product constructor in the creator code, modify it to the reference of the factory method, and migrate the code creating the product to the factory method.

    Small message

A person’s struggle, like pregnancy, will always be seen after a long time.

Life is short. I don’t want to pursue what I can’t see. I just want to catch what I can see.

I amSay, thank you for reading. If it helps you, please like it and forward it. Thank you.