introduce

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

  • AbstractFactory: AbstractFactory role that declares a set of methods for creating a product, one for each product.

  • ConcreteFactory: ConcreteFactory role that implements the method of creating a product defined in an abstract factory, producing a set of concrete products. The concrete product components become a product category, each in a product hierarchy.

  • AbstractProduct: Abstracts product roles, declaring interfaces for each product.

  • ConcreteProduct: ConcreteProduct roles that define ConcreteProduct objects produced by factories and implement business methods for abstract product interface declarations.

case

Transmitter interface

/ / the engine
public interface Engine {

    void run(a);

    void start(a);
}

Copy the code

The engine implements class EngineA

public class EngineA implements Engine{

    @Override
    public void run(a) {
        System.out.println("Turn fast!");
    }

    @Override
    public void start(a) {
        System.out.println("Quick start, automatic."); }}Copy the code

The engine implements class EngineB

public class EngineB implements Engine{

    @Override
    public void run(a) {
        System.out.println("Slow turn!);
    }

    @Override
    public void start(a) {
        System.out.println("Quick start, manual."); }}Copy the code

Seat interface

/ / seat
public interface Chair {

    void run(a);
}
Copy the code

The chair implements the class ChairA

public class ChairA implements Chair{
    @Override
    public void run(a) {
        System.out.println("Self-heating!"); }}Copy the code

The chair implements the class ChairB

public class ChairB implements Chair{
    @Override
    public void run(a) {
        System.out.println("No heating!"); }}Copy the code

The car factory

public interface CarFactory {

    // Create engine
    Engine createEngine(a);

    // Create the seat
    Chair createChair(a);
}
Copy the code

Car factory implementation class

public class JiLiFactory implements CarFactory {
    @Override
    public Engine createEngine(a) {

        return new EngineA();
    }

    @Override
    public Chair createChair(a) {

        return newChairA(); }}Copy the code

The client

public class Client {

    public static void main(String[] args) {
        CarFactory carFactory=newJiLiFactory(); Engine engine=carFactory.createEngine(); engine.run(); engine.start(); }}Copy the code

Advantages and disadvantages

Advantages:

1) Reduce coupling: Abstract factory mode delays the creation of specific products to subclasses of specific factories. In this way, the creation of objects is encapsulated, which can reduce the dependence between clients and specific product classes, so that the coupling degree of the system is low, which is more conducive to later maintenance and expansion; 2) More in line with the open-closed principle: when a new product class is added, only the corresponding specific product class and the corresponding factory subclass need to be addedCopy the code

Disadvantages:

1) The biggest disadvantage of the abstract factory pattern is that product family extension is very difficultCopy the code

Applicable scenarios:

1) A system is not required to rely on the expression of how product class instances are created, composed, and expressed, which is a prerequisite for all factory pattern applications. 2) The system has multiple series of products, and only one series of products is consumed in the systemCopy the code



Github Demo address: ~ ~ ~ portal ~ ~ ~

Personal blog address: blog.yanxiaolu.cn /