[design pattern] Abstract Factory

Time:2021-8-25

Abstract factory of design pattern

In the previous article, we learned about simple factories. We know that simple factories are places where different classes are created. How are these factories created? With the increase of our business logic, we may need many such simple factories. We can’t take the initiative to create each one. Although this can also be achieved, it is not elegant. Moreover, the later maintainers can’t distinguish the key points of modification, resulting in one hair and the whole body.

Next, learn how to create simple factories in one way. Even if many factories are needed, you can create them in this way to reduce the coupling of code and improve its cohesion.

Both abstract factory and factory method are used to solve the problem of interface selection, but in implementation, abstract factory is a central factory used to create patterns of other factories.

Different implementation services (classes / servers) have different methods, so an interface adaptation is required. This adaptation class is equivalent to a factory in a factory, which is used to create different services abstracted into a unified interface to do the same business.

Demo

Business:

Suppose there are two logistics companies a and B, both of which have the business of shipping and picking up goods. At the same time, companies a and B can independently carry out different kinds of shipping and picking up, that is, they can be customized. If there are these two companies, it is very simple. We can use simple factories and create two new factories. However, if there are 10 or 50 companies, it is a little inappropriate for us to create factories. At this time, we have to use abstract factories.

Abstract factory

The defined Abstract factories are abstract classes, which define the abstract methods of shipping and picking up, and return abstract classes.

/// <summary>
    ///Abstract factory
    /// </summary>
    public abstract class AbstractFactory
    {
        /// <summary>
        ///Delivery
        /// </summary>
        /// <returns></returns>
        public abstract ASendGood SendGood();

        /// <summary>
        ///Receiving
        /// </summary>
        /// <returns></returns>
        public abstract AGetGood GetGood();
    }
/// <summary>
    ///Pick up abstract class
    /// </summary>
    public abstract class AGetGood
    {
        public abstract void GetGood();
    }
    
    /// <summary>
    ///Shipment abstract class
    /// </summary>
    public abstract class ASendGood
    {        
        public abstract  void SendGood();
    }
/// <summary>
    ///Company a abstract factory
    /// </summary>
    public class CompanyAAbstractFactory : AbstractFactory
    {
        public override AGetGood GetGood()
        {
            return new CompanyAGetGood();
        }

        public override ASendGood SendGood()
        {
            return new CompanyASendGood();
        }
    }

    /// <summary>
    ///Abstract factory of company B
    /// </summary>
    public class CompanyBAbstractFactory : AbstractFactory
    {
        public override AGetGood GetGood()
        {
            return new CompanyBGetGood();
        }

        public override ASendGood SendGood()
        {
            return new CompanyBSendGood();
        }
    }
public class CompanyAGetGood : AGetGood
    {
        public override void GetGood()
        {
            Console.writeline ("company a pick up");
        }
    }
    
    public class CompanyASendGood : ASendGood
    {
        public override void SendGood()
        {
            Console.writeline ("shipped by company a");
        }
    }
class CompanyBGetGood : AGetGood
    {
        public override void GetGood()
        {
            Console.writeline ("company B pick up");
        }
    }
    class CompanyBSendGood : ASendGood
    {
        public override void SendGood()
        {
            Console.writeline ("company B delivery");
        }
    }

After defining the abstract factory and its respective abstract classes, the next step is to call.

static void Main(string[] args)
        {
            Debug. Writeline ("use the abstract factory to realize the respective delivery and pick-up of logistics companies a and B");
            //Company a
            AbstractFactory aCompanyFactory = new CompanyAAbstractFactory();
            AGetGood getGood = aCompanyFactory.GetGood();
            getGood.GetGood();

            ASendGood sendGood = aCompanyFactory.SendGood();
            sendGood.SendGood();
            //Company B
            AbstractFactory bCompanyFactory = new CompanyBAbstractFactory();
            getGood = bCompanyFactory.GetGood();
            getGood.GetGood();

            sendGood = bCompanyFactory.SendGood();
            sendGood.SendGood();
            Console.ReadKey();
        }

[design pattern] Abstract Factory
You can see from the above picture that the picking up and shipping contents of their companies are displayed after operation. In fact, for their respective logistics companies, they don’t care about the specific picking and delivery process at all. They just give actions, so the abstract factory will automatically find the corresponding implementation for implementation.

Abstract factory pattern: it provides an interface for creating products to create related or dependent objects without specifying specific classes

Benefits of abstract factories

  • Transfer the creation of the product to the subclass of the specific factory, encapsulate the creation of the object, and reduce the dependence between the client and the specific product class.
  • Reduce the system coupling, which is conducive to expansion and later maintenance.

For the abstract factory, it is still very abstract to understand. Only by tapping it yourself can it be better familiar with and used. However, if you want to use it, you still have to find a specific business scenario that conforms to it. Sometimes, don’t design for design, just use it reasonably.

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.