Home  >  Article  >  Java  >  What is abstract factory pattern

What is abstract factory pattern

PHP中文网
PHP中文网Original
2017-06-22 14:06:011835browse

This article talks about the last type of factory, let’s start with

Abstract Factory PatternDefinition(from Baidu Encyclopedia):
Abstract Factory Pattern is a factory pattern of all forms The most abstract and general form. The abstract factory pattern refers to a factory pattern used when there are multiple abstract roles.
The abstract factory pattern can provide an interface to the client, allowing the client to create product objects in multiple product families without specifying the specific product. According to the Liskov substitution principle,
Any place that accepts a parent type should be able to accept a subtype. Therefore, what the system actually needs is just some instances of the same type as these abstract product roles, not instances of these abstract products.
In other words, they are instances of concrete subclasses of these abstract products. Factory classes are responsible for creating instances of concrete subclasses of abstract products.

UML class diagram

Specific code:

public class Client {public static void main(String[] args) {//        Creator benzCreator = new BenzCreator();//        benzCreator.createCar().run();////        Creator bmwCreator = new BmwCreator();//        bmwCreator.createCar().run();AbstractFactory factory1 = new Factory1();
        factory1.createProductA().getProductAName();
        factory1.createProductB().getProductBName();

        AbstractFactory factory2 = new Factory2();
        factory2.createProductA().getProductAName();
        factory2.createProductB().getProductBName();
    }
}public interface AbstractFactory {
    AbstractProductA createProductA();

    AbstractProductB createProductB();
}public class Factory1 implements AbstractFactory {
    @Overridepublic AbstractProductA createProductA() {return new ProductA1();
    }

    @Overridepublic AbstractProductB createProductB() {return new ProductB1();
    }
}public class Factory2 implements AbstractFactory {

    @Overridepublic AbstractProductA createProductA() {return new ProductA2();
    }

    @Overridepublic AbstractProductB createProductB() {return new ProductB2();
    }
}public interface AbstractProductA {
       String getProductAName();
}public class ProductA1 implements AbstractProductA {
    ProductA1(){
        System.out.println("产品A1");
    }

    @Overridepublic String getProductAName() {return "产品A1名称";
    }
}public class ProductA2 implements AbstractProductA {
    ProductA2(){
        System.out.println("产品A2");
    }

    @Overridepublic String getProductAName() {return "产品A2名称";
    }
}public interface AbstractProductB {
    String getProductBName();
}public class ProductB1 implements AbstractProductB {
    ProductB1(){
        System.out.println("产品B1");
    }

    @Overridepublic String getProductBName() {return "产品B1名称";
    }
}public class ProductB2 implements AbstractProductB {
    ProductB2(){
        System.out.println("产品B2");
    }

    @Overridepublic String getProductBName() {return "产品B2名称";
    }
}

Product family:

refers to a family of products with related functions located in different product hierarchical structures

Specific Example:

Let me give you an example that may not be appropriate
For example, our computers are both 32-bit and 64-bit, and the corresponding machines install software with corresponding bits
The two CDs are stored separately When installing 32-bit and 64-bit software, you only need to get a CD to install the whole machine software. You don't have to search for each software one by one.
The disc here is the specific factory. If each disc has QQ and 360 (what about fighting?)
The 32-bit disc here is Factory1 in the above code. The 64-bit disc here is Factory2 in the above code.
QQ in the 32-bit CD is ProductA1, 360 is ProductB1
QQ in the 64-bit CD is ProductA2, 360 is ProductB2

For a novice user like me, just get the right CD. , you don’t need to find both the correct QQ digit and the 360 ​​digit, isn’t it great?

Advantages and Disadvantages
Advantages:
Separate the interface and implementation, so that the client does not need to know the specific implementation.
It becomes easy to switch product families, just like if a 128-bit machine appears, it is just a matter of adding an extra CD.

Disadvantages:
It is not easy to expand new products (imagine how many classes and methods you need to add to add a product).
The hierarchical structure of the class is complex (looking at the class diagram makes you want to vomit)

Essence:

I said before that the essence of the factory method is Choosing implementation is to choose a specific product
The abstract factory is to choose a product family, changing from a single product to a product family.

Now that we have finished talking about the three factory models, we will first dig a hole into the connection, difference and evolution process between them, and then write about them later.

The above is the detailed content of What is abstract factory pattern. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:JAVA sorting heap sortNext article:JAVA sorting heap sort