Home Java javaTutorial java factory pattern

java factory pattern

Dec 15, 2016 pm 01:51 PM
java factory pattern

There are three types of Java factory patterns: simple factory pattern, factory method pattern, and abstract factory pattern.

Simple Factory Pattern is an innovative pattern of classes, also called Static Factory Method Pattern. It is responsible for creating instances of other classes by specifically defining a class. The created instances usually have Common parent class.

The simple factory pattern uses a "omnipotent class" to decide which specific class of objects to create based on the information passed by the outside world. As shown in the picture below

java factory pattern

uses a simple factory class to create the corresponding product object based on the name.

The simple factory pattern seriously violates the "opening and closing principle" and is difficult to expand, thus giving rise to the factory method pattern.

FACTORY METHOD is an abstraction of the simple factory pattern, conforms to the "opening and closing principle", and achieves scalability.

The meaning of the Factory Method pattern is to define a factory interface that creates product objects, deferring the actual creation work to subclasses. The core factory class is no longer responsible for the creation of products. In this way, the core class becomes an abstract factory role, responsible only for the interfaces that specific factory subclasses must implement. The benefit of further abstraction is that the factory method pattern allows the system to operate without modifying the specific factory role. Introducing new products.

Use scenarios of factory method pattern:

For example, a foundry that assembles mobile phones. Obtain accessories such as casing, display screen, motherboard, buttons, and battery from the mobile phone raw material factory for assembly. The mobile phone assembly factory is only responsible for the assembly of mobile phones, not the production of accessories. It does not need to care whether the accessories coming out of the mobile phone raw material factory have changed, as long as the interfaces connecting the various configurations of the mobile phone remain unchanged. For example, if the display screen of the raw material factory is changed from TFT to UFB display, for the foundry that assembles mobile phones, as long as the interface remains unchanged, it only needs to continue assembly.

As shown in the figure below, first create a raw material factory interface. The raw material factory interface defines the methods for creating the casing, display screen, motherboard, and battery respectively.

java factory pattern

The LB mobile phone raw material factory is responsible for providing the accessories needed for LB mobile phones, including batteries, TFT displays, motherboards, and leather casings.

java factory pattern

The following is the interface of the mobile phone assembly factory. The assembly factory is only responsible for obtaining accessories from the raw material factory and assembling them into new mobile phones. It is not responsible for anything else.

java factory pattern

LB mobile phone assembly factory is responsible for assembling accessories provided by LB raw material factory. Of course, I occasionally take on private work, assembling mobile phones from other mobile phone manufacturers.

java factory pattern

java factory pattern

LB mobile phone sales are very popular, and there are assembly plants all over the country. One day, the screen of LB's mobile phone suddenly had to be changed, from a TFT display to a UFT display. A single move affects the whole body, and the benefits of the factory method model are reflected at this time. No matter how the configuration of the LB mobile phone changes, for the assembly factory, the createDisplay() interface remains unchanged. This only requires that the display screen of the raw material factory be changed from TFT display screen to UFT display screen.

The advantage of the factory pattern is that it only needs to provide the created product interface to the user. No matter how the product type changes, as long as the function of the product created based on the interface does not change, the user does not need to make any changes.

Many introductions to the factory method pattern list the usage of the factory pattern, but do not specifically introduce when to use it. When I first read it, I felt that the factory method pattern was redundant and added complexity. Later, I gradually became familiar with the usefulness of the factory method pattern and read "Head First Design Patterns" again to consolidate it. I simulated a scene and recorded it with theory, hoping to consolidate understanding.



For more articles related to java factory pattern, please pay attention to PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

See all articles