How to Build a Personal‑Contract Lantu Pay SDK: Design Patterns, Request Flow, and Implementation Details
This article continues the open‑source design series by detailing the remaining design points of the Lantu Pay SDK, including the end‑to‑end request chain, the Post method with retry support, the use of strategy and template method patterns, and a custom @Require annotation for mandatory parameter validation.
2.1 SDK Request Real Chain
The article provides a demo project (https://github.com/wuchubuzai2018/lantu-pay-sdk) that shows how a client creates a payment QR code in fewer than ten lines. After defining a test request, the Service layer’s order‑creation method is called, which first invokes checkAndSign for parameter validation and signing, then builds the request URL and converts the request to a form‑body string (e.g., key1=value1&key2=value2) as required by Lantu Pay’s Open API, and finally sends the request via a post method.
The post method constructs an Executor object and calls its execute method, which adds an outer layer to support HTTP error retry. The subsequent executeInternal method delegates to the HTTP request executor, achieving decoupling between business code and the underlying HTTP framework.
2.2 Design Patterns Used in the SDK
The SDK heavily employs the Strategy pattern and the Template Method pattern to keep the code simple and eliminate conditional branches. An abstract class AbstractWxPayRequest serves as the base for all API requests; concrete request classes inherit from it and implement business‑specific methods.
The abstract class defines an unimplemented storeMap method, which acts as the hook for the Template Method pattern, forcing subclasses to provide their own implementation while the parent class orchestrates the overall flow. The HTTP request executor Executor follows a similar template, offering default OKHTTP implementations for GET and POST while allowing custom overrides for special business needs, inspired by the wxjava project.
2.3 Required‑Parameter Validation Design
The SDK adopts an annotation‑based validation similar to wxjava. A custom @Require annotation is defined and placed on request fields that must be present.
During request parameter checking, reflection scans all fields, detects the @Require annotation, and performs the necessary validation.
Summary of Learnings
The article highlights six key takeaways:
Unified abstract HTTP request framework design and implementation.
Multiple Template Method, Strategy, and Builder pattern applications.
Annotation‑driven @Require validation for mandatory parameters.
Error‑retry mechanism design.
Generic type design.
Request signature design.
Readers are encouraged to contribute to the open‑source project, extend missing features, and apply these design techniques to improve their own SDK development and interview portfolios.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Ubiquitous Tech
A ubiquitous public account for pirate enthusiasts, regularly sharing curated experiences, tech learning, and growth insights. Currently publishing articles on AI RAG customer service, AI MCP technology, and open-source design. Personal free Knowledge Planet: Awakening New World Programmer.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
