One, foreword

This is the fourth article in a series exploring the art of Android SDK development. This paper introduces the design idea of streaming API to implement the custom option configuration requirements of SDK elegantly.

Series of articles:

Android SDK Development Art Exploration (1) Introduction and Design

(2) Exception or ErrorCode

Android SDK development art exploration (iii) initialization

Android SDK development art exploration (4) personalized configuration

Android SDK development art exploration (5) security and verification

Android SDK development art exploration (6) compression and optimization

Android SDK development art exploration (vii) Dependency principles and packaging methods

2. SDK custom configuration

2.1. What is custom configuration

A common requirement in SDK development is to provide a set of configuration methods for customizing SDK behavior. Such as switching debug/formal mode, enabling/disabling certain functions.

2.2. Design a configuration method

Earlier we introduced the concept of custom configuration and referred to the custom configuration method of a push SDK implementation. I believe we can also achieve their OWN SDK configuration according to this idea!

However, this way is not too satisfying, we usually development time can also see a very cool Java code call way, just take an example:

StringBuilder builder = new StringBuilder();
builder.append("one").append("two").append("three").length();
Copy the code

As you can see, continuous streaming calls to the API are convenient and concise. This API implementation method, also called Fluent Interface, is an implementation method of object-oriented API in software engineering. So the question is, why not use such good API design ideas in our SDK to make developers feel better?

Let’s review the essence of SDK configuration: The essence of SDK configuration methods is to provide default configurations for SDK-related functions and receive custom configurations from developers to modify the default logic. So our method includes not only the default option, but also the modification method. Without further ado, first a template example:

Configuration method

/**
 * <pre>
 *     @author  : bruce
 *     @time    : 2020/07/10
 *     @desc    : MySDKConfig
 *     @version : 1.0
 * </pre>
 */
public class MySDKConfig {

    // Default configuration
    private static boolean sDebug = false;
    private static long sTimeout = 8000L;

    private static final MySDKConfig.Config CONFIG = new MySDKConfig.Config();

    public static class Config {
        private Config(a) {}/** * Set the debug mode **@paramIsDebug mode *@return Config
         */
        public MySDKConfig.Config setDebug(final boolean isDebug) {
            sDebug = isDebug;
            return this;
        }

        /** * Set timeout **@paramTimeout Indicates the timeout period *@return Config
         */
        public MySDKConfig.Config setTimeout(final long timeout) {
            // Here is an example of how boundary values are handled
            long minTimeout = 3000L;
            if (timeout < minTimeout) {
                sTimeout = minTimeout;
            } else {
                sTimeout = timeout;
            }
            return this; }}public static boolean isDebug(a) {
        return sDebug;
    }

    public static long getTimeout(a) {
        return sTimeout;
    }

    public static MySDKConfig.Config getConfig(a) {
        returnCONFIG; }}Copy the code

Invoke the sample

// One line of code, streaming calls
MySDKConfig.getConfig().setDebug(true).setTimeout(8000L);
Copy the code

As you can see from the source example, we provide some default configurations. The custom configuration is implemented through static inner classes, and getter methods are provided in the outer layer to provide the configuration to other SDK modules to call.

The key to implementing streaming calls is that the This object itself is returned in each setter method, thus implementing the streaming API.

In SDK development scenarios, it is especially suitable to use streaming API configuration to build custom configurations because there are many configurations to be configured and default configurations are involved. In fact, on second thought, this design idea is actually a simplified version of the Builder mode of the use scenario?

Three, endnotes

To review, this paper briefly introduces the realization of SDK custom configuration, that is, through the design idea of streaming API interface, combined with the actual requirements of SDK development, designed a set of STREAMING call (also known as chain call) SDK personalized configuration implementation scheme.

Finally, if this document is helpful or inspiring to your development, like/follow/share three lines is the best incentive for the author to continue to create, thanks for your support!

Refer to the article

Fluent Interface

Copyright Notice:

This article first appeared in my column AndDev Android development has been authorized to hongyang public account exclusive release.