What is abstract factory?

We’ve looked at simple factory design patterns, factory design patterns, today we’re going to look at abstract factory design patterns. Compare them and tell them how different they are.

What is a drawline factory?

An abstract factory is not only an abstract factory, but also an abstract product, and there are multiple products to be created. Therefore, the abstract factory corresponds to multiple real factories, each of which is responsible for creating multiple real products. Drawing line factories are used to solve more complex problems.

An interface is a factory responsible for creating a related object without explicitly specifying its class. Each generated factory can provide objects according to the factory pattern.

Case 2.

There is now an abstract factory that makes shapes of various colors. There are colors, there are shapes, and together they are a factory. Color and shape are factories in themselves. Take a look at the UML diagram:

The code is shown in the following figure:

/** ** color */
public interface IColor {

    /** * fill color */
    void fill(a);
}

public class Red implements IColor {
    @Override
    public void fill(a) {
        System.out.println("Fill it with red"); }}public class Green implements IColor {
    @Override
    public void fill(a) {
        System.out.println("Fill green"); }}public class Blue implements IColor {
    @Override
    public void fill(a) {
        System.out.println("Fill blue"); }}Copy the code

The shape of

public interface IShape {
    void draw(a);
}


/** * circle */
public class Circle implements IShape {

    @Override
    public void draw(a) {
        System.out.println("Draw a circle."); }}/** * square */
public class Square implements IShape {

    @Override
    public void draw(a) {
        System.out.println("Draw a square."); }}/** * rectangle */
public class Rectangle implements IShape {

    @Override
    public void draw(a) {
        System.out.println("Draw a rectangle."); }}Copy the code

The abstract factory

/** * Abstract factory interface class */
public interface AbstractFactory {

    / / color
    IColor getColor(String type);

    / / shape
    IShape getShape(String type);
}
Copy the code

Color the factory

/** ** color factory */
public class ColorFactory implements AbstractFactory {

    @Override
    public IColor getColor(String type) {
        if ("red".equals(type)) {
            return new Red();
        } else if("blue".equals(type)) {
            return new Blue();
        } else {
            return newGreen(); }}@Override
    public IShape getShape(String type) {
        return null; }}Copy the code

Shape of the factory

package com.lxl.www.designPatterns.abstractFactoryPattern.shapeColorFactory;

/** * shape factory */
public class ShapeFactory implements AbstractFactory {


    @Override
    public IColor getColor(String type) {
        return null;
    }

    @Override
    public IShape getShape(String type) {
        if ("circle".equals(type)) {
            return new Circle();
        } else if("square".equals(type)) {
            return new Square();
        } else {
            return newRectangle(); }}}Copy the code

Factory producer

/** * factory producer */
public class FactoryProductor {

    // Abstract factory
    AbstractFactory getFactory(String type) {
        if ("color".equals(type)) {
            return new ColorFactory();
        } else if ("shape".equals("type")) {
            return new ShapeFactory();
        } else {
            return newShapeFactory(); }}}Copy the code

When to produce something, you have to follow instructions, so pass in a parameter. Finally, look at client calls

public static void main(String[] args) {
        FactoryProductor factoryProductor = new FactoryProductor();
        AbstractFactory shapeFactory = factoryProductor.getFactory("shape");
        IShape circle = shapeFactory.getShape("circle");
        circle.draw();

        AbstractFactory colorFactory = factoryProductor.getFactory("color");
        IColor red = colorFactory.getColor("red");
        red.fill();
    }
Copy the code

Running results:

Draw a circle

Fill in the red

First, I want a shape first, so I get the shape factory and let the shape factory make a prototype. Then, I’m going to color the shape, go to the color factory, find the color, and fill it.

This is the drawline factory, the factory of the factory. The materials in the factory need to be obtained from other factories.

The abstract factory pattern is designed to keep the creation of a factory and a set of products separate from use and can be switched to another factory and another set of products at any time.

The key point of the abstract factory pattern implementation is to define the factory interface and the product interface, but how to implement the factory and the product itself is left to the concrete subclass implementation, the client only deals with the abstract factory and the abstract product.

Advantages and disadvantages of abstract factory

Advantages:

The abstract factory pattern addresses the creation of a set of objects with the same constraints. Among them, the core concept is product family and product grade structure. Abstract factory model is very easy to expand product grade structure. It only needs to add new product class and new factory class

Disadvantages:

If you want to expand the product family, that is, to add a product, you need to create the corresponding class, and at the same time need to modify all factories, which is not close on the principle.

Four. The difference between simple factory, factory mode and drawing line factory mode

Simple Factory Design pattern:

Factory design mode:

Drawing line factory design mode:

As can be seen from the UML diagram,

  1. The simple factory design pattern, where the factory creates a single object.
  2. Factory mode, for each function to create a factory, easy to expand, extensible ability is stronger than simple factory, code structure is more complex
  3. Abstract factory design pattern, factory of factory. All the products in the factory are produced by other factories, which products are needed, tell the drawing line factory, the abstract factory creates products in the specified concrete product factory.