What is abstract factory pattern
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.
