Home  >  Article  >  Java  >  What is the factory pattern in java

What is the factory pattern in java

王林
王林Original
2019-11-11 15:07:534550browse

What is the factory pattern in java

What is factory pattern?

Factory mode is our most commonly used instantiation object mode. It is a mode that uses factory methods to replace new operations. This type of design pattern is a creational pattern, which provides an optimal way to create objects.

In the factory pattern, we do not expose the creation logic to the client when creating an object, and point to the newly created object by using a common interface.

Intent:

Define an interface for creating objects and let its subclasses decide which factory class to instantiate. The factory pattern delays the creation process to the subclasses. conduct.

Mainly solve:

Mainly solve the problem of interface selection.

Advantages of the pattern:

1. If a caller wants to create an object, he only needs to know its name. 2. High scalability. If you want to add a product, you only need to extend a factory class. 3. Shield the specific implementation of the product, and the caller only cares about the product interface.

Disadvantages of the pattern:

Every time a product is added, a specific class and object implementation factory need to be added, doubling the number of classes in the system. To a certain extent, it increases the complexity of the system and also increases the dependence on specific classes of the system. This is not a good thing.

Note:

As a class creation pattern, the factory method pattern can be used wherever complex objects need to be generated. One thing to note is that complex objects are suitable for using the factory pattern, while simple objects, especially those that can be created only through new, do not need to use the factory pattern. If you use the factory pattern, you need to introduce a factory class, which will increase the complexity of the system.

Recommended tutorial: Java tutorial

The above is the detailed content of What is the factory pattern in java. 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