Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the factory method model

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about what is the factory method model, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following contents for you. I hope you can gain something according to this article.

The factory method pattern is a commonly used class creation design pattern. The core spirit of this pattern is the changing part of the encapsulation class, in which the individualized and fickle part is extracted as an independent class. Through dependency injection to achieve decoupling, reuse and facilitate later maintenance and expansion.

Introduction to the pattern:

The meaning of the factory method (Factory Method) pattern is to define a factory interface to create product objects, deferring the actual creation to subclasses. The core factory class is no longer responsible for product creation, so the core class becomes an abstract factory role, only responsible for the interfaces that must be implemented by the specific factory subclass. the advantage of this further abstraction is that the factory method pattern enables the system to introduce new products without modifying the specific factory role.

The factory method pattern is a derivative of the simple factory pattern, which solves many problems of the simple factory pattern. First of all, the 'open-closed principle' is fully realized, and the extensibility is realized. Secondly, the more complex hierarchical structure can be applied to the situations where the product results are complex. [2]

The factory method pattern abstracts the simple factory pattern. There is an abstract Factory class (which can be abstract classes and interfaces), this class will no longer be responsible for specific product production, but will only develop some specifications, and the specific production work will be done by its subclasses. In this pattern, factory classes and product classes can often correspond in turn. That is, an abstract factory corresponds to an abstract product, and a concrete factory corresponds to a specific product, and this concrete factory is responsible for producing the corresponding products.

Factory method pattern (Factory Method pattern) is the most typical template method pattern (Template Method pattern) application.

Role structure:

Abstract factory (Creator) role: is the core of the factory method pattern, independent of the application. The factory class of any object created in the schema must implement this interface.

Concrete factory (Concrete Creator) role: this is a concrete factory class that implements an abstract factory interface, contains logic that is closely related to the application, and is called by the application to create product objects. There are two such roles in the above figure: BulbCreator and TubeCreator.

Abstract product (Product) role: the supertype of the object created by the factory method pattern, that is, the common parent class or interface of the product object. In the figure above, the character is Light.

Concrete Product role: this role implements the interface defined by the abstract product role. A specific product is created by a specific factory, and they often correspond to each other one by one.

Pattern application:

The factory method is often used in the following two situations:

The first situation is that for a product, the caller clearly knows which specific factory service should be used, instantiates the specific factory, and produces the specific product. This is the case with the iterator () method in Java Collection.

In the second case, you just need a product, and you don't want to know or need to know which factory is produced, that is, it is up to the producer to decide which specific factory to choose. They instantiate a specific factory back to the user according to the current system, and the decision-making process is transparent to the user.

After reading the above, do you have any further understanding of what is the factory method model? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report