Home > Backend Development > PHP Tutorial > What is factory pattern?

What is factory pattern?

藏色散人
Release: 2023-04-06 13:10:02
Original
11490 people have browsed it

Factory mode is the most commonly used instantiation object mode. It is a mode that uses factory methods to replace new operations. The factory pattern has a key construct, which is a static method named Factory according to general principles. However, this is just a principle. Although the factory method can be named arbitrarily, this static method can also accept parameters of any data and must return an object.

What is factory pattern?

Factory pattern is our most commonly used instantiation object pattern. It is a pattern that uses factory methods to replace new operations. It is widely used in the famous Jive forum. The factory pattern can be seen everywhere in the Java program system.

Because the factory pattern is equivalent to new for creating instance objects, we often generate instance objects based on class Class, such as A a=new A(). The factory pattern is also used to create instance objects, so in the future when new You need to be careful and consider whether you can consider using factory mode. Although doing so may require more work, it will bring greater scalability to your system and require as few modifications as possible.

What is factory pattern? Factory pattern definition

We take class Sample as an example. If we want to create an instance object of Sample:

Sample sample=new Sample();
Copy after login

However, the actual situation is that usually we have to create a sample instance Do some initialization work, such as assigning values ​​to query the database, etc.

First of all, what we think of is that we can use the constructor of Sample, so that the generated instance is written as:

Sample sample=new Sample(参数);
Copy after login

However, if the initialization work done when creating the sample instance is not as simple as assignment It may be a long piece of code. If it is also written into the constructor, your code will be ugly (refactoring will be required).

Why is the code said to be ugly? Beginners may not feel this way. Our analysis is as follows. If the initialization work is a long piece of code, it means that there is a lot of work to be done. Packing a lot of work into one method is quite It is very dangerous to put many eggs in one basket. This is also contrary to Java's object-oriented principles. Object-oriented encapsulation (Encapsulation) and dispatch (Delegation) tell us to "cut" long code assignments as much as possible. into each segment, and then "encapsulate" each segment (to reduce the coupling and connection between segments). In this way, the risks will be dispersed. If modifications are needed in the future, just change each segment, and there will be no more disturbing incidents. matter.

In this example, first of all, we need to separate the work of creating an instance from the work of using an instance, that is, separating the large amount of initialization work required to create an instance from the Sample constructor.

At this time we need the Factory mode to generate objects, and we can no longer use the simple new Sample (parameter) above. Also, if Sample has an inheritance such as MySample, according to interface-oriented programming, we need to abstract Sample into an interface. Sample is an interface and has two subclasses, MySample and HisSample. When we want to instantiate them, it is as follows:

ISample mysample=new MySample();
ISample hissample=new HisSample();
Copy after login

As the project progresses, Sample may "give birth to many sons", so we need to instantiate these sons one by one. What's worse, we may have to modify the previous code: add sons later. Example. This is unavoidable in traditional programs.

But if you consciously use the factory pattern from the beginning, these troubles will be gone.

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