The abstract factory pattern of the factory pattern

Summer is here, I’m driving a BMW, but I can’t see my girlfriend’s happy smile?

Me: honey, what’s wrong with you? I’m not happy.

Girl friend: you see this hot day of, we this car meng of panic, although you open a BMW with one hand is very cool, but I already hot of have no mood to see you drive.

I love my girlfriend so much that IN order to make her happy again, I went to BMW and asked them to put an air conditioner in my car.

Abstract factory model of factory model

1.1 introduction

As customers became more demanding, BMWS needed air conditioning. So the factory started making BMWS and the air conditioners they needed.

At this time the factory had two series of products: BMW and air conditioning.

The BMW must use the corresponding air conditioning to use. At this time, if the use of a car factory and an air conditioning factory can not meet our needs, we must confirm the corresponding relationship between the car and air conditioning. Therefore, the car factory and air conditioning factory are linked together. Hence the abstract factory pattern.

Arguably, the difference between the abstract factory pattern and the factory method pattern is the level of complexity required to create objects. And the abstract factory pattern is the most abstract and generic of the three.

1.2 Abstract factory pattern composition

Abstract the roles of the factory pattern (basically the same as the factory method) :

  1. Abstract Factory role: This is the core of the factory method pattern and is application-independent. Is an interface that a specific factory role must implement or a parent class that must inherit. In Java it is implemented by abstract classes or interfaces.

  2. Concrete factory role: It contains code related to specific business logic. Objects called by the application to create corresponding product-specific objects.

  3. Abstract product family role: It is the parent class or interface that the concrete product inherits.

  4. Specific product roles: Objects created by specific factory roles are instances of this role.

1.3 Abstract factory pattern class diagram

BMW went back to the drawing board to redesign the design:

1.4 Abstract factory pattern implementation

After having the design drawings, BMW reconfigured their company, mainly re-standardizing the company. The factory not only produced cars, but also air conditioners of corresponding models.

1.4.1 Abstract Product Role BMW

Abstract product role: It is the parent class or interface that the concrete product inherits. In Java, there are usually abstract classes or interfaces.

BMW is the same as the factory method mode, here is the code directly attached (see the factory method mode section for details on why it is designed this way) :

Abstract product role: It is the parent class or interface that the concrete product inherits. In Java, there are usually abstract classes or interfaces.

BMW is the same as the factory method mode, here is the code directly attached (see the factory method mode section for details on why it is designed this way) :

package com.kfit.factory.bmw.abstractfactory; / * * * * * BMW superclass * * * * @author "SpringBoot" * * @date 2020-11-19 * * @slogan slogan "Slogan" public abstract class BMW {public BMW(){system.out.println ("BMW made - part of..."){public BMW(){system.out.println ("BMW made - part of...") ); } /\*\* * definition abstract method: all BMWS should be able to run. \* \*/ public abstract void run(); }Copy the code

Here we have another air-conditioner product, and we also need to define Aircondition:

package com.kfit.factory.bmw.abstractfactory; /\*\* \* @author "SpringBoot" \* @date 2020-11-19 \* @@slogan Slogan is a simple slogan in nature \*/ public abstract class Aircondition {// Corresponding abstract method, not defined here.}Copy the code

1.4.2 Product Roles BMW520 or BMW521

Specific product roles: Objects created by specific factory roles are instances of this role. Implemented in Java by concrete classes.

BMW520/BMW521 is the same as the factory method mode, here is the code directly attached (see the factory method mode section for details) :

BMW520: * * * *

package com.kfit.factory.bmw.abstractfactory; /\*\* \* BMW520 \*\* @author \* @date 2020-11-19 \* @@slogan Slogan "public class BMW520 extends" \*/ BMW {public BMW520(){system.out.println (" BMW520 "); } / / Override public void run(){system.out.println ("... ); }}Copy the code

BMW521: * * * *

package com.kfit.factory.bmw.abstractfactory; /\*\* \* BMW520 \*\* @author \* @date 2020-11-19 \* @@slogan Slogan "public class BMW521 extends" BMW {public BMW521(){system.out.println (" BMW521 "); } / / Override public void run(){system.out.println ("... ); }}Copy the code

The realization of the corresponding air conditioning products: Aircondition520 / Airconodition521:

Aircondition520:

package com.kfit.factory.bmw.abstractfactory; /\*\* \* model 520 air conditioner \*\* @author \ "public number SpringBoot" \* @date 2020-11-19 \* @slogan slogan great Simplicity in the sky \*/ public class Aircondition520 extends Aircondition { }Copy the code

Aircondition521:

package com.kfit.factory.bmw.abstractfactory; / * * * * * 521 model air conditioner * * * * @author realize "public number SpringBoot" * * @date 2020-11-19 * * @slogan great Simplicity in the sky */ public class Aircondition521 extends Aircondition { }Copy the code

1.4.3 Abstract the Factory Role BMWFactory

Abstract Factory role: This is the core of the factory method pattern and is application-independent. Is an interface that a specific factory role must implement or a parent class that must inherit. In Java it is implemented by abstract classes or interfaces.

BMW has specified a set of standards for all companies. Each factory needs to have a capacity to produce cars (createBMW) and air conditioners (createAirc). However, your car and air conditioner production process is not much interference from headquarters.

BMWFactory can be either an abstract class or an interface. An interface is a case that can be satisfied.

package com.kfit.factory.bmw.abstractfactory; /\*\* \* @author \* @date 2020-11-19 \* @slogan Slogan slogan slogan \*/ public interface BMWFactory { / \ * \ * \ * method of specializing in the production of BMW @ return \ \ * * / BMW createBMW (); / * @return */ Aircondition createAirc(); }Copy the code

1.4.3 role BMWFactory520 / BMWFactory521 concrete factory

Concrete factory role: It contains code related to specific business logic. Objects called by the application to create corresponding product-specific objects.

For the two series of BMW 520 and BMW 521, for the convenience of management (the procedure is to expand capacity), an air conditioner specially produced for BMW 520 and supporting 520 was established in Beijing, and an air conditioner specially produced for BMW 521 and supporting 521 was established in Shanghai.

BMWFactory520:

package com.kfit.factory.bmw.abstractfactory; /\*\* \* BMW 520 factory \*\* @author "SpringBoot" \* @date 2020-11-19 \* @slogan Avenue to simple realization in nature \*/ public class BMWFactory520 implements BMWFactory { @Override public BMW createBMW() { return new BMW520(); } @Override public Aircondition createAirc() { return new Aircondition520(); }}Copy the code

BMWFactory521:

package com.kfit.factory.bmw.abstractfactory; / * * * * BMW 521 factory * * * * @author "SpringBoot" * * @date 2020-11-19 * * @slogan slogan big Simple in nature */ public class BMWFactory521 implements BMWFactory { @Override public BMW createBMW() { return new BMW521(); } @Override public Aircondition createAirc() { return new Aircondition521(); }}Copy the code

** 1.5ME -** Car is coming

BMW: for our SVIP customers, our new models already support air conditioning. We offer you a discount.

Me: Ok, right away.

In order to surprise my girlfriend, I didn’t tell her, so I went to pick up the car:

package com.kfit.factory.bmw.abstractfactory; /\*\* \* I - Customer \*\* @author \ \ "SpringBoot" \* @date 2020-11-19 \* @slogan slogan "Slogan" \*/ public class Me { Public static void main(String\[\] args) {/\*\* * \*/ / new BMWFactory = new BMWFactory520(); BMW bmw = factory.createBMW(); Aircondition aircondition = factory.createAirc(); // When the bus comes, I take my girlfriend for a walk bmw.run(); Factory521() = new BMWFactory521(); bmw = factory.createBMW(); aircondition = factory.createAirc(); // When the bus comes, I take my girlfriend for a walk bmw.run(); //aircondition.run()Copy the code

When I pulled up to my girlfriend in my air-conditioned car, she grinned from ear to ear and gave me a big hug. I’m happy, too. Dear, love you ten thousand years.

Air-conditioned car in hand, summer is not hot, winter is not cold, with a girlfriend said to go.