Home > Common Problem > body text

What does abstract factory pattern mean?

hzc
Release: 2020-06-29 15:55:53
Original
2922 people have browsed it

The abstract factory pattern is to create other factories around a super factory. The super factory is also called the factory of other factories. This type of design pattern is a creational pattern, which provides the best way to create objects. Way.

What does abstract factory pattern mean?

#Abstract Factory Pattern is to create other factories around a super factory. The Gigafactory is also known as the factory of other factories. This type of design pattern is a creational pattern, which provides an optimal way to create objects.

In the abstract factory pattern, the interface is a factory responsible for creating a related object without explicitly specifying their class. Each generated factory can provide objects according to the factory pattern.

Introduction:

Intent: Provide an interface for creating a series of related or interdependent objects without specifying their specific classes.

Main solution: Mainly solve the problem of interface selection.

When to use: The system's products have more than one product family, and the system only consumes products from one of the families.

How to solve: Define multiple products in a product family.

Key code: Aggregate multiple similar products in a factory.

Application example: After working, in order to attend some parties, you must have two or more sets of clothes, such as business clothes (complete sets, a series of specific products), fashion clothes (complete sets, a series of specific products) ), even for a family, there may be business women's clothing, business men's clothing, fashionable women's clothing, fashionable men's clothing, these are also complete sets, that is, a series of specific products. Assume a situation (which does not exist in reality, otherwise, there is no way to enter communism, but it is helpful to illustrate the abstract factory model). In your home, a certain wardrobe (concrete factory) can only store a certain kind of such Clothes (set, a series of specific products), every time you pick up this set of clothes, you will naturally take them out of this wardrobe. Use OOP thinking to understand that all wardrobes (concrete factories) are wardrobes (abstract factories), and each set of clothes includes specific tops (a specific product), pants (a specific product) product), these concrete tops are actually tops (an abstract product), and the concrete pants are also pants (another abstract product).

Advantages: When multiple objects in a product family are designed to work together, it can ensure that the client always only uses objects in the same product family.

Disadvantages: Product family expansion is very difficult. To add a certain product of a series, you need to add code to both the abstract Creator and the specific one.

Usage scenarios: 1. Change skins on QQ, and change the whole set together. 2. Generate programs for different operating systems.

Note: Product families are difficult to expand, but product levels are easy to expand.

Implementation:

We will create Shape and Color interfaces and entity classes that implement these interfaces. The next step is to create the abstract factory class AbstractFactory. Then define the factory classes ShapeFactory and ColorFactory, both of which extend AbstractFactory. Then create a factory creator/generator class FactoryProducer.

AbstractFactoryPatternDemo, our demo class uses FactoryProducer to obtain AbstractFactory objects. It will pass the shape information Shape (CIRCLE / RECTANGLE / SQUARE) to the AbstractFactory in order to get the type of object it needs. At the same time, it also passes color information Color (RED / GREEN / BLUE) to AbstractFactory in order to obtain the type of object it requires.

What does abstract factory pattern mean?

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template