Factory method pattern of Android design pattern series

Factory method mode is often the mode for beginners of design mode. Indeed, some people call it the most typical and enlightening mode.

Too many factory classes are used in Android, among which the factory method pattern is useful. Of course, many factories do not use the factory method pattern, but just tool management classes.

Today, take threadfactory as an example to illustrate the simple factory pattern and factory method pattern.

Factory method pattern, factory method, is a simple way, not a simple application.

1. Intention

Define an interface for creating objects and let subclasses decide which class to instantiate. The factory mode pattern delays the instantiation of a class to its subclasses. Popular vocabulary: fictitious maker delays creating object subclasses

2. Structure diagram and code

Let's first look at the standard factory method structure diagram:

First, the abstract product class and the abstract factory class, and then use the specific factory of the client to produce the corresponding specific products, but the client does not know how the specific products are produced, and the production process is encapsulated in the factory. Therefore, to some extent, the factory method pattern has changed the way we directly create objects with new. It is a good start and of great significance.

Take threadfactory as an example:

@H_ 502_ 32@

In fact, this diagram is slightly different from the original structure diagram, that is, the parameterized factory is also somewhat different from the business sense, but the idea is the same. Let's look at the specific code:

The following is the specific implementation:

For example, the specific implementation of the factory in the asynctask class is as follows:

On the one hand, we sigh for the convenience of its production, and on the other hand, we feel cumbersome to create a factory without creating a certain kind of products. Therefore, we introduce a simple factory below. Its structure diagram is as follows:

The simple factory removes the abstract factory, and you can create one that specializes in producing certain products. In some specific and irresponsible fields, it is very practical and convenient to apply this model. This class is used in the connection class in Android:

The abstract class connection serves as both an abstract product class and a concrete factory class.

In this case, we often need to produce subclasses immediately. Getconnection methods are often static, so simple factories are also called static factory methods. Let's look at the code as follows:

This is a simple factory, a very simple parameter chemical factory, really simple.

3. Effect

1. Creation mode;

2. Get the corresponding object from the parameter chemical plant method mode;

3. Provide hooks for subclasses;

4. Connect parallel class hierarchies.

The above is the factory method mode of Android design mode series introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>