Moment For Technology

Design pattern series abstract factory

Posted on Dec. 1, 2022, 5:14 p.m. by 張承翰
Category: The back-end Tag: The back-end Design patterns

"This is the second day of my participation in the More Text Challenge. For more details, see more text Challenge."

Abstract factory of design patterns

Last time we looked at simple factories, and we know that simple factories are places where different types are created, so how are these factories created? With the increase of our business logic, we may need a lot of such simple factories, we can not take the initiative to create each one, although it can be achieved, but it is not elegant and late maintainers are not clear about the focus of modification, resulting in a touch.

Next, learn how to create simple factories in one way, so that even if you need many factories, you can create them in this way, making your code less coupled and more cohesive.

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

Different implementation services (classes/servers) are partially different in the way they are implemented, so an interface adaptor is needed. This adaptor class acts as a factory within a factory, creating an interface that abstracts different services to do the same business.

Demo

Business:

Assuming that there are two logistics companies A and B, they both 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 goods, that is to say, they can be customized. If we have these two companies it's easy, we can use a simple factory and just create two new factories, but if we have 10 or 50 companies, it's a little inappropriate to create a factory, so we have to use an abstract factory.

The abstract factory

The abstract factory defined is an abstract class, which defines the abstract method of shipping, fetching, and returning the abstract class.

// /summary public abstractClass AbstractFactory {/// /summary public abstractclass AbstractFactory {/// /summary public abstractclass AbstractFactory {/// /summary public abstractclass AbstractFactory {/// /summary public abstractclass AbstractFactory {/// /summary public abstractclass AbstractFactory {/// /summary returns/returns public abstract ASendGood SendGood(); Public abstract AGetGood GetGood(); // returns/returns public abstract AGetGood GetGood(); }Copy the code
/// summary public abstract class AGetGood {public abstract void GetGood(); /summary public abstract class ASendGood {public abstract void SendGood(); }Copy the code
/// /summary public class CompanyAAbstractFactory: AbstractFactory { public override AGetGood GetGood() { return new CompanyAGetGood(); } public override ASendGood SendGood() { return new CompanyASendGood(); /summary /summary public class CompanyBAbstractFactory: AbstractFactory { public override AGetGood GetGood() { return new CompanyBGetGood(); } public override ASendGood SendGood() { return new CompanyBSendGood(); }}Copy the code
Public class CompanyAGetGood: AGetGood {public override void GetGood() {console. WriteLine(" CompanyAGetGood "); } class CompanyASendGood: ASendGood {public override void SendGood() {console. WriteLine(" companya "); }}Copy the code
Class CompanyBGetGood: AGetGood {public override void GetGood() {console. WriteLine(" CompanyBGetGood "); } class CompanyBSendGood: ASendGood {public override void SendGood() {Console. }}Copy the code

With the abstract factories and their respective abstract classes defined, it's time to call them.

Static void Main(string[] args) {Debug. // CompanyAAbstractFactory aCompanyFactory = new CompanyAAbstractFactory(); AGetGood getGood = aCompanyFactory.GetGood(); getGood.GetGood(); ASendGood sendGood = aCompanyFactory.SendGood(); sendGood.SendGood(); BAbstractFactory bCompanyFactory = new CompanyBAbstractFactory(); getGood = bCompanyFactory.GetGood(); getGood.GetGood(); sendGood = bCompanyFactory.SendGood(); sendGood.SendGood(); Console.ReadKey(); }Copy the code

As you can see from the picture above, the operation shows the pick-up and delivery content of the respective companies. In fact, for their respective logistics companies, they do not care about the specific pickup and delivery process, just give the action, then the abstract factory will automatically find the corresponding implementation to carry out the implementation.

Abstract factory pattern: Provides an interface to create a product that is responsible for creating related or dependent objects without explicitly specifying concrete classes

Benefits of abstract factories

  • Moving the creation of the product into subclasses of the concrete factory encapsulates the creation of the object and reduces the dependency between the client and the concrete product class.
  • Reduce the coupling degree of the system, which is conducive to expansion and later maintenance.

For the abstract factory, it is still very abstract to understand, only their own hands to tap again in order to better familiar with and use. But to use it, you still have to find a specific business scenario that fits it. Sometimes don't design for design's sake, just use it.

Small remarks

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

Life is short, I don't want to pursue what I can't see, I just want to grasp what I can see.

I am zai said, thank you for reading, if it is helpful to you, trouble point like, forwarding thank you.

Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.