Experience in designing interfaces

From business independence

If business A is closely related to business B, business A and business B are completely independent businesses. At this time, separate interfaces should be designed for service A and service B. Instead of providing only one interface to bind services A and B together.

For example, the following two businesses:
1. Face liveness detection
2. Face comparison

The two are closely related. In many scenarios, the liveness detection is performed first to ensure the authenticity of the face, and then the face comparison is performed.

But "living detection" and "face comparison" are businesses that can exist independently. That is to say, there can be only "living detection" or "face comparison" (for example, uploading a personal photo to query the person's identity information).

If only designed as an interface.

/**
*识别人脸
*/
void identifyFace() {
        先活体检测,
        再人脸比对
}

The above is designed like this, although most scenarios are applicable. However, "live detection" and "face comparison" are bound together. If you want to do "face comparison", you must first do "live detection". It is no longer possible to do "face comparison" alone.

A better approach should be to set interfaces for the two services separately.

/**
*活体检测
*/
void detectFace();

/**
*人脸比对
*/
void matchFace ()

Confirm that the parameters make sense

The more common ones are internal order numbers and external order numbers. In some projects, the internal order number is generated according to rules, and the specified records can be found quickly by following this rule when querying. The external order number also has such rules, but it is different from the rules of the internal order number, so the mapping relationship between the internal order number and the external order number must be maintained.

But if there is no such rule for internal order number or external order number, in fact, there must be only one order number.

Interfaces should be atomic

An interface is only responsible for a single function. If a business only requires the design of one interface but calls multiple functions, then the atomic interface should be encapsulated twice and packaged into one interface.

So there are two types of interfaces, one is a business interface and the other is a single-function interface. A business interface can be realized by combining multiple functional interfaces.

If there is a functional overlap between the two interfaces, it means that the two interfaces are designed incorrectly.

Consider the performance of the business interface

If the business interface is composed of a single functional interface, the performance is low. It is necessary to consider expanding the functional interface.
For example, the following situation:
the functional interface is to import a single picture, and the business interface requires importing pictures in a certain directory, and there may be more than 1,000 pictures in the directory.

Assuming that the following interface is an AIDL interface, the Android client needs to read more than 1,000 pictures into the memory, and then call the following interface more than 1,000 times in a loop. Moreover, more than 1,000 pictures are transmitted to the server across processes, and the performance is obviously too low.

/**
* 导入单张图片
*/
void importBitmap(Bitmap bmp);

At this time, the following functional interface should be expanded:

/**
* 批量导入图片
* @param bmpDir 图片所在的目录路径
*/
void batchImportBitmaps(String bmpDir);

In this way, the Android client only needs to call once, and the image can be read by the Android server, avoiding 1000 cross-process transmissions.

Guess you like

Origin blog.csdn.net/jiejingguo/article/details/124197372