Detailed explanation of factory method mode example of Android programming design mode

This paper illustrates the factory method pattern of Android programming design pattern. Share with you for your reference, as follows:

1、 Introduction

Factory pattern is one of the creative design patterns. Factory method mode is a simple structure mode, which is widely used in our usual development. Maybe you don't know, but you have used this mode countless times, such as various life cycle methods in activity in Android. Take oncreate method as an example, it can be regarded as a factory method, In it, we can construct our view and return it to the framework for processing through setcontentview. We will talk about the relevant contents later. Let's take a look at the factory method pattern definition first.

2、 Definition

Define an interface for creating objects and let subclasses decide which class to instantiate.

3、 Usage scenario

Factory method patterns can be used wherever complex objects need to be generated. Complex objects are suitable for using factory mode. New can be used to create objects without using factory mode.

4、 Simple implementation of pattern

Abstract product class:

Specific product category A:

Specific product category B:

Abstract factory class:

Specific plant type:

Customer category:

result:

The roles here are very simple. They are mainly divided into four modules. One is the abstract factory, which is the core of the factory method pattern; Second, specific factories, which implement specific business logic; The third is the abstract product, which is the parent class of the product created by the factory method pattern; The fourth is the concrete product, which is the object of a specific product to realize the abstract product.

In the above code, we construct a factory object in the client class and produce a product object through it. The product object we get here is an instance of concreteproducta. If you want to get an instance of concreteproductb, you can change the logic in ConcreteFactory:

This method is common. You can produce any product you need. Sometimes you can use reflection to produce specific product objects more concisely. At this time, you need to pass a class class into the parameter list of the factory method to determine which product class:

For a specific factory class, you can obtain an example of the class through reflection:

Finally, let's look at the implementation in the client:

This method is simple and dynamic. If you don't like this method, you can also try to define a specific factory for each product and perform their respective duties.

The method of having multiple factories like this is called the multi factory method pattern. Similarly, go back to our original factory method pattern. When there is only one factory, we still provide an abstract class for the factory. Can we simplify it? If you are sure that there is only one factory class, it is certainly no problem to simplify the abstract class. We only need to change the corresponding factory method to a static method:

This approach, also known as simple factory pattern or static factory pattern, is a weakened version of the factory method pattern.

In fact, you can find that the factory method pattern fully conforms to the design principles, which reduces the coupling between objects. Moreover, the factory method pattern depends on the abstract architecture, and the instantiation task is completed by subclasses, which has very good scalability.

5、 Factory method pattern in Android source code

Various lifecycles of activity

ArrayList and HashSet

6、 Summary

advantage:

The factory method pattern fully conforms to the design principles and reduces the coupling between objects. The high-level module only needs to know the abstract class of the product, and other implementations don't need to care.

Good encapsulation and clear code structure. Good scalability.

Disadvantages:

Every time we add a new product to the factory method pattern, we write a new product class. At the same time, the abstract layer should be introduced, which will inevitably lead to the complexity of class structure. Therefore, in some cases, it is necessary for designers to weigh the advantages and disadvantages of using factory pattern.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

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