Abstract the definition of the factory pattern

Provides an interface for creating a set of related or interdependent objects without specifying their concrete classes

Generic class diagrams for abstract factory patterns

Abstract factory pattern is an upgraded product of factory method pattern. When there are multiple business varieties and business classification, it is a very good solution to generate the required objects through abstract factory pattern.

Abstract factory pattern generic source code class diagram

First of all, there are two product lines (also called product families) that interact with each other, such as the left door and right door of a car. These two should be the constraints between the two objects in equal number —-. The doors of each model are different, which is the structural constraint of the product level. Look at the source code implementation below:

Abstract product class A

package com.zxf.srp.factory3; Public abstract class AbstractProductA {public void shardMethod(){} public void shardMethod(){} Different implementations public abstract void doSomething(); }Copy the code

Realization of product A1

Public class ProductA1 extends AbstractProductA {@override public void doSomething() {system.out.println ();  }}Copy the code

Realization of product A2

Public class ProductA2 extends AbstractProductA {@override public void doSomething() {system.out.println ();  }}Copy the code

Abstract product class B

Public abstract class AbstractProductB {public void shardMethod(){} public void shardMethod(){ Different implementations public abstract void doSomething(); }Copy the code

Realization of product B1

Public class ProductB1 extends AbstractProductB {@override public void doSomething() {system.out.println (" ProductB1 ");  }}Copy the code

Realization of product B2

Public class ProductB2 extends AbstractProductB {@override public void doSomething() {system.out.println (" ProductB2 ");  }}Copy the code

The abstract factory class is responsible for defining the functionality to be implemented by each factory. In common code, an Abstract factory defines product creation for two product families.

Abstract Factory class

Public abstract class AbstractCreator {public abstract AbstractProductA createProductA(); Public abstract AbstractProductB createProductB(); }Copy the code

How to create a product is left up to the concrete implementation classes.

Implementation classes for product level 1

public class Creator1 extends AbstractCreator { @Override public AbstractProductA createProductA() { return new ProductA1(); } @Override public AbstractProductB createProductB() { return new ProductB1(); }}Copy the code

Product level 2 implementation classes

public class Creator1 extends AbstractCreator { @Override public AbstractProductA createProductA() { return new ProductA2(); } @Override public AbstractProductB createProductB() { return new ProductB2(); }}Copy the code

Note: for M product levels there should be M implementation class factories, in each implementation factory. Implement production tasks for different product groups

Scene: the class

Public class Client {public static void main(String[] args){AbstractCreator creator1 = new creator1 (); AbstractCreator creator2 = new Creator2(); AbstractProductA productA1 = creator1.createProducta (); AbstractProductB productB1 = creator1.createProductB(); AbstractProductA productA2 = creator1.createProducta (); AbstractProductB productB2 = creator1.createProductB(); }}Copy the code

In the scenario class, there is no method related to the implementation class. For a product, we only need to know its factory method to directly produce a product object, regardless of its implementation class.

Concrete examples

In the case of Nuwa before our transformation, she needed to distinguish between men and women. Here is the class diagram:

The class diagram is large, but relatively simple. One interface, multiple abstract classes, and then N implementation classes. Each person is an abstract class, and gender is implemented in each implementation class.

A code example is as follows:

Public void getColor(); /** * public void getColor(); Public void getLanguage(); Public void getSex(); public void getSex(); }Copy the code
Public abstract class AbstractWhiteHuman implements Human {@override public void getColor() { System.out.println(" White people have white skin "); } @override public void getLanguage() {system.out.println (); }}Copy the code
Public abstract class AbstractBlackHuman implements Human {@override public void getColor() { System.out.println(" Black people have black skin "); } @override public void getLanguage() {system.out.println (" black "); }}Copy the code
/** * public abstract class AbstractYellowHuman implements Human {@override public void getColor() { System.out.println(" Yellow skin is yellow "); } @override public void getLanguage() {system.out.println (); }}Copy the code

Each abstract class has two implementation classes that implement the most detailed, concrete things in common.

/** * eyelash eyelash {public class eyelash eyelash {@override public eyelash eyelash () { System.out.println(" Yellow woman "); }}Copy the code
/** * public class eyelash eyelash extends abstractyelash eyelash {@override public eyelash eyelash () { System.out.println(" yellow male "); }}Copy the code

The concrete realization of the white and black man is omitted, as is the yellow man.

Public interface HumanFactory {// Create a HumanFactory public Human createYellowHuman(); Public Human createWhiteHuman(); Public Human createBlackHuman(); }Copy the code

In the interface, we can produce people of different skin colors. The specific production of people of different genders needs to be realized in specific factories.

/** * public class FemaleFactory implements HumanFactory {// Create FemaleFactory @override public Human createYellowHuman() { return new FemaleYellowHuman(); } @override public createWhiteHuman() {return new FemaleWhiteHuman(); } @override public createBlackHuman() {return new FemaleBlackHuman(); }}Copy the code
Public implements HumanFactory {public implements HumanFactory @override public implements HumanFactory () { return new MaleYellowHuman(); } @override public createWhiteHuman() {return new MaleWhiteHuman(); } @override public createBlackHuman() {return new MaleBlackHuman(); }}Copy the code
NvWa {public static void main(String[] args){HumanFactory maleFactory = new MaleFactory(); // HumanFactory femaleFactory = new femaleFactory (); / / production line set up, to start producing people Human yellowMaleHuman = maleFactory. CreateYellowHuman (); Human yellowFemaleHuman = femaleFactory.createYellowHuman(); System.out.println("---- produces a yellow female -----"); yellowFemaleHuman.getColor(); yellowFemaleHuman.getLanguage(); yellowFemaleHuman.getSex(); System.out.println("---- produces a yellow male -----"); yellowMaleHuman.getColor(); yellowMaleHuman.getLanguage(); yellowMaleHuman.getSex(); }}Copy the code
Results: ---- gave birth to a yellow woman ----- yellow woman with yellow skin who speaks Chinese ---- gave birth to a yellow man ----- yellow man with yellow skin who speaks ChineseCopy the code

Males and females of all colors were created, male manufacturing factories only produced males, female manufacturing factories only produced females, and later it was easy to add gender, just create a factory to produce.

The application of abstract factories

Advantages of the abstract factory pattern

1. Encapsulation, the implementation class of each product is not the concern of high-level modules, it is concerned with the interface, is abstract. Objects are made by factory classes, and we can create a desired object simply by knowing who the factory class is. 2. Constraints within a product family are in a closed state. For example, when it comes to the male-to-female ratio, there should be a constraint that for every female, there should be a male, so that the production process is transparent to the higher-level module calling the factory and he doesn’t need to know about this constraint.

Disadvantages of the abstract factory pattern

1. The biggest disadvantage of abstract factory mode is that it is very difficult to expand the product family. Let’s take the general code as an example. CreateProductC (), and then both implementation classes are modified, which seriously violates the open close principle.

Use scenarios for abstract factories

The use scenario of the abstract factory pattern is very simple. If a family of objects has the same constraints, you can use the abstract factory pattern.

Considerations for abstract factories

The abstract factory pattern is difficult to extend, but keep in mind that it is the product family that is difficult to extend, not the product level. In the example above, it is easy to add a skin color and difficulty while adding a gender.

Special thanks to

Zen of Design Patterns

PS: if you think the article is not wrong, you can add the public account to pay attention to the article will be synchronized to the public account.