Multi-type business processing design skills

zhaozj2021-02-16  47

There will often be multi-type business processing work in enterprise application development, and a good design method will bring great benefits to applications and maintenance, and we start from a simple case.

A book sales system is handled in customer payment:

Classification: Ordinary consumers, general members, VIP members, other types of pending.

Treatment requirements: ordinary consumers do not enjoy discounts

General members enjoy 15% discount

VIP members enjoy 20% discount, plus accumulated points

Other types to be determined

Let's implement it below. When we implement, most people think of writing a method (function) to the settlement operation, we will write this way in the function:

String ls_customer_type

Double LD_Payment

// Cancer type

LS_CUSTOMER_TYPE = ............

IF ls_customer_type = 'g' Then // General consumer

// processing process

Elseif Ls_Customer = 'V' // VIP Consumer

// processing process

Elseif

.....

END IF

// Other processing

Return LD_PAYMENT

Or use the Choose Case instead of the IF ELSEIF statement, the structure is tilted.

Such treatment methods are not very convenient for increased, reading, and new types of new types. If every type of processing has a lot of line code (dozens of lines to the hundred lines), everyone is more difficult to read, add a new type, you need to modify the method, the code will be longer, each processing The variables used are also very prone to cross, and the program is faulty is difficult.

In the face of the above questions, my design method is to create a method separately for each type, providing an identical interface, and a method requires a method to receive requests and return results. In this way, whether it is clear when debugging or running, a method corresponds to a process, increasing a new type, adding a method does not need to modify other method content, and the program is relatively relaxed.

Specific design implementation:

Create a settlement processing object: UO_BALANCE

Balance's method: of_getpayment (as_customer_type) // External Access Processing

OF_GETGENERALPAYMENT () // ordinary customer settlement processing

OF_GETVIPPAYMENT () // VIP member settlement processing

Of_otherpayment () // Other Processing

OF_GETPAYMENT method implementation:

转载请注明原文地址:https://www.9cbs.com/read-27310.html

New Post(0)