Factory pattern of software design pattern (Java) Java read-write configuration file — a brief note on the use of the properties class

What is the factory model?

Factory mode is the most commonly used instantiation object mode. It is a mode that uses factory method instead of new operation. The famous Jive forum makes extensive use of factory mode, which can be seen everywhere in Java program system. Because factory mode is equivalent to creating new instance objects, we often need to generate instance objects according to class. For example, a = new a() factory mode is also used to create instance objects. Therefore, we need to pay more attention to new in the future. Can we consider using factory mode? Although doing so, we may do more work, But it will bring greater scalability and less modification to your system.

To put it simply, the factory pattern is to provide a manufacturer that generates instantiated objects, Each factory will provide similar objects (implement common interfaces). When we need to instantiate an object of a class, we don't need to care about how the server obtains the object. We can directly put forward our requirements to the corresponding factory (realize the separation of client and server).

The factory model is divided into three categories:

1. Simple factory mode

2. Factory method

3. Abstract factory pattern

Generally, we classify the simple factory pattern as the factory method pattern, and then the abstract factory pattern is an extension of the factory method pattern.

1. Factory method model

Let's first look at the composition of the factory method mode:

Factory role: This is the core of this model and contains certain business logic and judgment logic. In Java, it is often implemented by a concrete class. Abstract product role: it is generally the parent class inherited by a specific product or the interface implemented. In Java, it is implemented by interfaces or abstract classes. Specific product role: the object created by the factory class is an instance of this role. It is implemented by a concrete class in Java.

Write a small example to simulate the event that God created man. God created three kinds of human beings with different skin colors according to different geographical locations and climates: yellow, white and black.

This is a skin color interface (here we roughly think that they only have skin color difference)

Humans with three skin colors are three implementation classes (implementing skininterface interface). Due to the consistent code structure, only one is given here

Then, if we want to get the object corresponding to the implementation class according to our previous practice, we need skininterface Si = new yellowrace(); In this way, although there is no problem on the surface, what about the explosion of a class of implementation classes? For example, there are hundreds of implementation classes. In addition to adding corresponding implementation classes, you also need to "display calls" on the client to expose the class name of the implementation class, which is also troublesome in later maintenance.

Then our factory class comes in handy

Write an implementation class and try it

From the above figure, we have achieved the basic purpose, but the previous problems already exist. Although we have solved the "display call" on the client, there is a pile of if on the server Judging from else if, if there are many implementation classes, you need more if The efficiency of elseif judgment is obviously low, because every time the client makes a request, the server has to make redundant judgment.

Is there any better way? That must be true. Java has a powerful mechanism - reflection. We can use the reflection mechanism to write all class names into a configuration file. In the future, we can simply maintain the configuration file. Let's see the specific implementation process.

First, we need a configuration file skin properties

Then write a class that reads the properties file and encapsulate the key = "value" into a map set. In this way, we only need to enter the keyword on the client to make the server automatically map to the corresponding class.

For reading configuration files in Java, unfamiliar friends can gently stamp "Java reading and writing configuration files - brief notes on the use of properties class" to help you summarize.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>