Abstract factory for design patterns

In the last article, we learned simple factories, know that simple factories are to create different kinds of places, so how to create these factories? With the increase of our business logic, we may need many such simple factories, and it is impossible for us to actively create each of them. Although this can be achieved, it is not elegant and the maintainer can not distinguish the key points of modification, resulting in the whole body being affected by a single action.

Learn how to create simple factories in a way that reduces the coupling of the code and makes it more cohesive, even if many factories are needed.

Both the abstract factory and the factory methods are intended to solve the interface selection problem, but in implementation, the abstract factory is a central factory that creates patterns for other factories.

Different implementation services (classes/servers) are different in some of the methods, so an interface adaptor is needed. This adaptor class is like a factory in a factory, which is used to create a factory that abstracts different services into a unified interface to do the same business.

Demo

Business:

Suppose there are two logistics companies A and B, both of which are engaged in the delivery and delivery of goods. Meanwhile, both companies A and B can independently deliver and pick up goods of different types, that is to say, they can be customized. If we have these two companies then it’s easy, we can use a simple factory, we can create two new factories, but if we have 10 or 50 companies, then it’s not appropriate for us to create a factory, so we have to use an abstract factory.

The abstract factory

An abstract factory is defined as an abstract class. It defines abstract methods for shipping and retrieving goods, and returns abstract classes.

// </summary> public abstract class AbstractFactory {/// </summary> public abstract class AbstractFactory {/// </summary> public abstract class AbstractFactory {/// </summary> public abstract class AbstractFactory {/// </summary> /// </summary> /// <returns></returns> public abstract ASendGood SendGood(); // </summary bb1 </summary bb2 </summary bb3 public summary AgetGood getGood (); }
// </ Summary > public summary class AgetGood {public abstract void GetGood(); } /// </summary summary> // </summary summary> public abstract class AsendGood {public abstract void SendGood(); }
/// </ Summary > // </ 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(); }}
Public class CompanyAgetGood: AgetGood {public Override void getGood () {Console.writeLine (); } public class CompanyAsEndGood: AsEndGood {public Override void SendGood() {Console.WriteLine(); }}
Class CompanyBGetGood: AgetGood {public Override void GetGood() {Console.WriteLine(); }} class CompanyBSendGood: AsendGood {public Override void SendGood() {Console.WriteLine(); }}

Having defined the abstract factory and its respective abstract classes, it’s time to call.

Static void Main(String [] args) {debug. writeLine (" Use abstract factory to deliver and pick up goods for Logistics Company A and B "); // company A abstractFactory acompanyFactory = new CompanyAabstractFactory (); AGetGood getGood = aCompanyFactory.GetGood(); getGood.GetGood(); ASendGood sendGood = aCompanyFactory.SendGood(); sendGood.SendGood(); // company B abstractFactory = new CompanyBabStractFactory (); getGood = bCompanyFactory.GetGood(); getGood.GetGood(); sendGood = bCompanyFactory.SendGood(); sendGood.SendGood(); Console.ReadKey(); }



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 pick-up and delivery process, but just give the action, then the abstract factory will automatically find the corresponding implementation to carry out.

Abstract factory pattern: provides an interface for creating products that are responsible for creating related or dependent objects, without specifying specific classes

The benefits of abstract factories

  • The creation of the product is transferred to the subclass of the specific factory, and the creation of the object is encapsulated to reduce the dependency between the client and the specific product class.
  • Reduce the coupling degree of the system, which is conducive to expansion and later maintenance.

For the abstract factory, the understanding is still very abstract, only their own hands to knock a better understanding and use. However, if you want to use it, you still have to find specific business scenarios in line with it. Sometimes, you can not design for the sake of design, but use it reasonably.

Small remarks

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

Life is short, and I don’t want to go for what I can’t see, I just want to catch what I can see.

I am zai say, thank you for reading, if you help, trouble thumb up, forward thank you.