Detailed explanation of abstract factory mode of Android programming design mode

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

1、 Introduction

Abstract factory pattern is also one of the creative design patterns. In the previous section, we have learned about the factory method pattern. What about this abstract factory? Let's think that factories in real life must be concrete, that is, each factory will produce a specific product. Isn't it strange that abstract factories mean that the products produced are uncertain? Abstract factory mode originated from the previous graphical solutions for different operating systems, such as buttons and text box controls in different operating systems, which have different implementations and different display effects. For each operating system, it itself constitutes a product class, while buttons and text box controls also constitute a product class, two product classes and two changes, Each has its own characteristics, such as button and textview in Android, button and textview in IOS, button and textview in window phone, etc.

2、 Definition

Provide an interface for creating a set of related or interdependent objects without specifying their specific classes.

3、 Usage scenario

The abstract factory pattern can be used when an object family has the same constraints. Does it sound abstract? For example, there are SMS software and dialing software under Android, IOS and window phone, both of which belong to the category of software software. However, their operating system platforms are different. Even the software produced by the same company has different implementation logic. At this time, you can consider using the abstract factory method mode to generate Android, IOS SMS software and dialing software under window phone.

4、 UML class diagram of abstract factory pattern

UML class diagram:

Although there are many kinds of abstract factory method patterns, they are mainly divided into four categories:

AbstractFactory: Abstract Factory role, which declares a set of methods for creating a product, and each method corresponds to a product.

ConcreteFactory: a concrete factory role, which implements the method of creating products defined in the abstract factory and generates a group of concrete products. These products constitute a product category, and each product is located in a product hierarchy.

Abstractproduct: abstract product role, which declares interfaces for each product.

Concreteproduct: concrete product role, which defines the concrete product object produced by the concrete factory and implements the business methods declared in the abstract product interface.

5、 Simple implementation

Taking the production of auto parts by the car factory as an example, a and B car factories produce different tires, engines and braking systems respectively. Although the parts produced are different, the models are different. But there are basically common constraints, namely tires, engines and braking systems.

Tire related categories:

Engine related classes:

Braking system related:

Abstract depot class:

Depot A:

Depot B:

Customer category:

result:

It can be seen that the above two car factories are simulated. If there are factory C and factory D, and the models and types of parts produced by their respective manufacturers are different, the class files we create will be doubled. This is also a disadvantage of the abstract factory pattern, so we should weigh the use in actual development.

6、 Difference from factory method mode

The previous section introduced the factory method pattern. What are the differences between them? The abstract factory pattern is an upgraded version of the factory method pattern. The comparison is as follows:

7、 Implementation in source code

The abstract factory pattern is rarely used in the Android source code, because there are few cases of multiple product categories, and most of them can be solved by using the factory method pattern.

MediaPlayer

Mediaplayer factory will generate four different mediaplayer base classes: stagefrightplayer, nuplayerdriver, midifile and testplayersub, all of which inherit from mediaplayerbase.

8、 Summary

advantage:

Separate the interface and implementation. The client uses the abstract factory to create the required objects, and the client does not know who the specific implementation is. The client is only product-oriented interface programming, which decouples it from the specific product implementation. At the same time, based on the separation of interface and implementation, the abstract factory method mode is more flexible and easy to switch product classes.

Disadvantages:

One is the explosive increase of class files

Second, it is not easy to expand new product categories

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