Software design-interface design, code optimization

Software design-interface design, code optimization

Preface

With the development of product business, the number of products and users continues to increase. Because each product corresponds to a set of interfaces, the number of interfaces is rapidly expanding, and the cost of maintenance is also increasing. Good interface design is particularly necessary in terms of code reuse and code robustness. It is also imperative to abstract a set of common interfaces from complex products.

At the beginning of the design

First of all, I studied the book of Big Talking Design Patterns. After unifying the cable design pattern, I analyzed it from the perspective of product design. It is particularly easy to use a simple factory, but the specific implementation details will be case whenjudged by the class , so the factory method is used to implement it, and the subclass Decide which class to instantiate and springget the corresponding object directly in the container. The
design mode used in this design has factory mode + command mode.

Start to achieve

  1. Define the interface base class ProductInterface, this class mainly contains the interface services required by most products, used as the basic implementation
  2. Define abstract class AbstractProductInterfaceimplementation ProductInterface, in the abstract class, you can provide basic services such as SMS sending
  3. Define the default implementation DefaultProductand extended implementation AppProduct, and select the corresponding product implementation from the container according to different product codes and channel sources
  4. The command mode is adopted to realize the isolation between service invocation and actual service realization interface, and realize the purpose of decoupling, which is realized by defining interfaces LoanCommandand specific service realization classes AppCreditApplyCommand.

Extension implementation

  1. Define the extended interface ExtendProductInterface, this class mainly inherits the interface base class ProductInterface, and adds special interface services for extension implementation
  2. Define abstract class AbstractExtendProductInterfaceimplementation ExtendProductInterface, in the abstract class, you can provide basic services such as SMS sending
  3. Define the default implementation DefaultExtendProduct, select the corresponding product implementation from the container according to different product codes and channel sources
  4. The command mode is adopted to realize the isolation between service invocation and actual service realization interface, and realize the purpose of decoupling, which is realized by defining interfaces LoanCommandand specific service realization classes ExtendOpenPersonAccountCommand.

Design thinking

Through this design, we have achieved the purpose of adding new services and only need to add new products to realize the functions. However, with the rapid development of functional design today, the practice of adding new product implementation classes every time a new product and service is added is still inelegant, which still will pass DefaultProductor DefaultExtendProductcreate dependency, a certain degree of coupling, there will be many xxxProduct.javacreated, but also provides a lot of the same features, such as 授信申请, 放款申请and so on, with the rapid growth in the number of products, from the initial two or three There is a tendency to expand to a dozen or even more than twenty. After thinking twice, I decided to optimize the interface design again.

Functional interface design

From the name point of view, it is very tall, but there is nothing tall in reality. The main implementation is Java8+based on the implementation of the basics. The implementation of functional programming is Lambdaan in-depth understanding and use of expressions, making the implementation of functions easier to read and Concise, reduce the coupling between the call and the callee

Start designing

  1. Define the functional interface service class FunctionProduct. The main method is ProductInterfacethe proxy implementation of the service in the interface. It is the specific implementation class that implements the functional method call. Through the delegation mode, the purpose of directly achieving the service call is realized, thereby achieving loose coupling
  2. Define the specific service realization, credit application AppCreditApplyFunctionand AppCreditLimitSelectFunctionthe specific service realization class
  3. Service expansion. From the perspective of the development of loan business, the emergence of new interfaces and new services is indispensable, but because we have implemented loose coupling here, we can ProductInterfaceadd public service doServicexxxdescriptions to the basic interface , which can be used in different products. , Dynamically implement specific doServicexxxservices. If it is a batch of brand-new services, it can be realized by extending the service interface, for
    example ExtendProductInterface, in the corresponding services of different products.
  4. Note: Due to the implementation of functional loose coupling, it is particularly important to achieve unified planning for product division and product corresponding service name division. Planning method:
    • With 产品名+productthe name of the product package
    • Named 产品名+接口中对应的服务名+Commandas a specific service implementation class
    • Use LoanCommandinterface executemethods as the implementation of specific services to facilitate future embedding and unified interception of service calls
  5. The purpose of the interface is to echo the specific service implementation of a specific product, which is conducive to code reading and expansion, and convenient for problem location

Design Optimization

Because the above design has realized the transformation from interface abstraction to functional aggregation in function beanName, the hard-coded problem is caused by the selection of the final service realization . In order to make the code more readable and flexible , Designed to obtain the corresponding beanNamefunctions from the interface parameters, what are the specific implementation ideas, and what specific implementations are there.
There are probably the following:

Implementation ideas:

   App 产品相关服务 BeanName 维护
   维护方式: 交易码 + productCode + channelId = 对应产品对应的服务名
   实现意义: 为了实现不同产品不同渠道来源对于不同服务的精确定位
   当前产品 1000 -> App  01-> 内部
   交易码描述,不同产品,交易码含义一样,可扩展交易码
   例如: ln1001 -> appCreditApply (授信申请)
   例如: ln1002 -> appCreditLimitSelect (额度查询)

Method to realize

  • The first: stored in the database
    • Option 1: Read directly from the database
    • Solution 2: Stored in the database, springloaded into the memory when the container starts
  • The second type: stored in memory
    • Option 1: javaSimilar to Map<key,value>storage in files
    • Solution 2: Store in the propertiesconfiguration filekey=value
    • Solution 3: If the project itself is integrated 携程 Apollo, online, service dynamic routing switch and service selection can be realized

beanNameGet

Define the attribute loading class BeanNameUtils, load the configuration information from the corresponding location when the container is started, and dynamically
obtain the corresponding according to the request parameter information after the specific transaction arrives beanName, which can realize the function of specific service selection.

  • The advantage is:
    • 1. Operate directly in the memory, which is more efficient
    • 2. Describe beanNameno redundant code in the configuration file , strong readability, unified maintenance, not easy to make mistakes
    • 3. If there are too many products, you can split, combine, add or delete configuration files according to the nature of the product, with strong scalability

Code

由于本文代码量比较大,请关注博主GitHub,获取完整代码

Code directory overview

Insert picture description here

Please see GitHub for the complete code and related dependencies

https://github.com/dwyanewede/project-learn/tree/master/src/main/java/com/learn/demo/interfaces/loan/product

Official account recommendation

Insert picture description here

Guess you like

Origin blog.csdn.net/shang_xs/article/details/90168527