Abstract Factory Pattern

1, Definition

Abstract Factory Pattern: one of the innovative patterns, which provides an interface to create a series of related or interdependent objects without specifying their specific classes.

2, UML class diagram

3, Roles and responsibilities

  • Abstract Product: it is the interface that defines the Product. It is the supertype of the object created by the factory method pattern, that is, the public parent class of the Product object.
  • Concrete Product: it implements the abstract product interface. A certain type of specific product is created by a special specific factory, and the specific factory corresponds to the specific product one by one.
  • Abstract Factory: Factory Method is declared in the abstract Factory class, which is used to return a product. Abstract Factory is the core of Factory Method pattern. All Factory classes that create objects must implement this interface.
  • Concrete Factory: it is a subclass of the abstract factory class. It implements the factory methods declared in the abstract factory and can be called by the client to return an instance of a specific product class.

4, Code implementation

preface:

Abstract basketball interface (abstract Product)

public interface BasketBall {
    void shot();
}

Adidas basketball (Concreate Product)

public class AdidasBasketBall implements BasketBall {
    @Override
    public void shot() {
        System.out.println("Use Adidas basketball to shoot");
    }
}

Nike basketball (Concreate Product)

public class NikeBasketBall implements BasketBall {
    @Override
    public void shot() {
        System.out.println("Shoot with Nike basketball");
    }
}

Abstract football interface (abstract Product)

public interface FootBall {
    void goal();
}

Adidas football (Concreate Product)

public class AdidasFootBall implements FootBall {
    @Override
    public void goal() {
        System.out.println("Shoot with Adidas football");
    }
}

Nike football (Concreate Product)

public class NikeFootBall implements FootBall {
    @Override
    public void goal() {
        System.out.println("Shoot with Nike football");
    }
}

Abstract Factory interface (abstract Factory class Factory)

public interface Factory {
    BasketBall makeBasketBall();
    FootBall makeFootBall();
}

Adidas factory (Concreate Factory)

public class AdidasFactory implements Factory {
    @Override
    public BasketBall makeBasketBall() {
        return new AdidasBasketBall();
    }

    @Override
    public FootBall makeFootBall() {
        return new AdidasFootBall();
    }
}

Nike factory (Concreate Factory)

public class NikeFactory implements Factory {
    @Override
    public BasketBall makeBasketBall() {
        return new NikeBasketBall();
    }

    @Override
    public FootBall makeFootBall() {
        return new NikeFootBall();
    }
}

Test class

public class AbstractFactoryTest {
    public static void main(String[] args) {
        Factory adidasFactory = new AdidasFactory();
        // Use Adidas basketball to shoot
        adidasFactory.makeBasketBall().shot();
        // Shoot with Adidas football
        adidasFactory.makeFootBall().goal();

        Factory nikeFactory = new NikeFactory();
        // Shoot with Nike basketball
        nikeFactory.makeBasketBall().shot();
        // Shoot with Nike football
        nikeFactory.makeFootBall().goal();
    }
}

5, Source code analysis

In MyBatis, there is a very typical abstract factory pattern.

SqlSession is the main Java interface of Mybatis

It has a default implementation class: DefaultSqlSession

SqlSessionFactory is an abstract factory interface used to create sqlsessions from data sources or database connections

SqlSessionFactory also has a default implementation class: DefaultSqlSessionFactory
We click the openSessionFromDataSource method to open the Session from the data source

DefaultSqlSession is actually returned

From the above source code, we can know that DefaultSqlSession is a specific product that implements the SqlSession abstract interface, and DefaultSqlSessionFactory is a specific factory that implements the SqlSessionFactory abstract interface and is used to produce DefaultSqlSession, which is the implementation of a typical abstract factory mode.

6, Analysis of advantages and disadvantages

advantage:

  • The code isolation of specific products in the application layer does not need the details of relationship creation.
  • Create a unified series of products together.

Disadvantages:

  • Adding a new product hierarchy is troublesome, and the original system needs to be greatly modified, and even the abstract layer code needs to be modified.

7, Applicable scenario

When the object to be created is a series of interrelated or interdependent product families, you can use the abstract factory pattern.

8, Summary

In an inheritance system, if there are multiple hierarchies (that is, there are multiple abstract classes) and there are certain associations or constraints between the implementation classes in each hierarchy, the abstract factory pattern can be used. If there is no association or constraint between the implementation classes in each hierarchy, it is more appropriate to use multiple independent factories to create products.

Keywords: Design Pattern

Added by tarlejh on Sat, 05 Mar 2022 09:33:07 +0200