Detailed explanation of builder mode example of Android programming design mode

This paper describes the builder mode of Android programming design mode. Share with you for your reference, as follows:

1、 Introduction

Builder pattern is a creation pattern that creates a complex object step by step. It allows users to more finely control the construction process of objects without knowing the internal construction details. The purpose of this pattern is to decouple the process of building complex objects from its components, so as to isolate the construction process from the representation of components.

Because a complex object has a large number of components, such as car, wheel, steering wheel, engine and various small parts, how to assemble these components into a car is a long and complex assembly process. In this case, in order to hide the implementation details from the outside during the construction process, The builder pattern can be used to separate the component from the assembly process, so that the construction process and components can be expanded freely, and the coupling between the two can be minimized.

2、 Definition

Separate the construction of a complex object from its representation, so that the same construction process can create different representations.

3、 Usage scenario

(1) The same method and different execution sequences produce different event results.

(2) Multiple assemblies or parts can be assembled into one object, but the running results are different.

(3) The product class is very complex, or the calling order in the product class has different effects. At this time, the builder mode is very appropriate.

(4) When initializing an object is particularly complex, for example, there are many parameters, and many parameters have default values.

4、 UML class diagram of builder pattern

Role introduction:

Product - abstract class of the product;

Builder - Abstract builder class, which standardizes the establishment of products. Generally, the specific establishment process is realized by subclasses;

Concreatebuilder -- specific builder class;

Director -- Unified assembly process;

5、 Simple implementation of builder pattern

The computer assembly process is complex and the assembly sequence is not fixed. In order to be easy to understand, we simplify the computer assembly process into three parts: building the host, setting the operating system and setting the display, and then build the computer object through director and specific builder.

Example code:

Output results:

In the above example, macbook objects are built through specific MacBook builder, and director encapsulates the process of building complex product objects and hides the construction details. Together with director, builder separates the construction of a complex object from its representation, so that different objects can be created in the same construction process.

It is worth noting that in the real development process, the director role is often omitted. A builder is directly used to assemble objects. This builder is usually called in a chain. The key point is that each setter method returns itself, that is, return this, so that the setter method can be called in a chain. The code is roughly as follows:

In this way, not only the director role is removed, the whole structure is simpler, but also the assembly process of product objects can be more finely controlled.

6、 Builder mode variant -- chain call

Example code:

Points to note:

The constructor of the user class is private, and the caller cannot directly create the user object.

The properties of the user class are immutable. All properties are added with the final modifier, and the value is set in the constructor. Moreover, only getters methods are provided externally.

The builder's internal class construction method only receives the required parameters, and the required parameters use the final modifier.

Call method:

Compared with the previous two methods of constructor and setter / getter method, it is more readable. The only possible problem is that it will generate redundant builder objects and consume memory. However, in most cases, our builder internal classes use static, so it doesn't matter much.

About thread safety

The builder mode is non thread safe. If you want to check the validity of a parameter in the builder internal class, you must check it after the object is created

Correct example:

Examples of errors:

7、 Examples of using builder mode

1. Alertdialog.builder in Android

2. Creation of okhttpclient in okhttp

3. Creation of retrofit objects in Retrofit

It can be seen that the director role is omitted in practical use. In many framework source codes, when it comes to the builder mode, most of them are not the builder mode of the classic GOF, but the latter with a simpler structure.

8、 Advantages and disadvantages

advantage:

Good encapsulation makes the client do not need to know the details of the internal implementation of the product

The builder is independent and has strong expansibility

Disadvantages:

Generate redundant builder objects and director objects and consume memory

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