Home > Java > javaTutorial > body text

java factory pattern

高洛峰
Release: 2016-12-15 13:51:35
Original
1527 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!