Decorative mode of grinding design mode-1

Source: Internet
Author: User
ArticleDirectory
    • 1.1 complex bonus Calculation
    • 1.2 simplified bonus Calculation System
    • 1.3 Non-use mode solution
    • 1.4 What's the problem?
Decorator (decorator) 1 Scenario problem 1.1 complex bonus Calculation

Consider a practical application: how to implement flexible bonus calculation.
Bonus calculation is a relatively complex function, especially for the calculation of bonuses for business departments. In addition to complicated business functions, another difficulty is that the calculation method often needs to change, because the business department often adjusts the calculation method of the bonus to boost morale.
First, let's look at the complexity of the existing bonus calculation method from the business perspective:

    • First, the bonus category: for individuals, there are roughly personal business bonuses for the current month, total personal bonuses, personal business growth bonuses, timely return bonuses, and limited-time transaction plus-code bonuses;
    • For business directors or business managers, in addition to individual bonuses, there are also accumulated team bonuses, business growth bonuses, and team profit bonuses.
    • The second is to calculate the bonus amount, and there are several bases: sales, sales gross profit, actual return, business costs, and bonus base;
    • The other formula is the calculation formula. The calculation formula is different for different people, different bonus classes, and different bonus amounts. Even if it is the same formula, the proportional parameters calculated in the table may also be different.

 

1.2 simplified bonus Calculation System

After reading the above bonus calculation questions, we are glad to learn the design model. We are not really going to implement the entire bonus calculation system business, therefore, there is no need to list all computing services here. To simplify the subsequent demonstration, the bonus calculation system used for the demonstration is as follows:

    • Each person's business bonus for the current month = Sales for the current month x 3%
    • Cumulative bonus for each person = total amount returned x 0.1%
    • Team bonus = total sales x 1%

 

1.3 Non-use mode solution

A person's bonus is divided into many parts. to calculate the bonus, the primary task is to calculate the bonus of each part according to the rules of each bonus, and then calculate the sum, this is the bonus that this person can get.
(1) For demonstration, first prepare test data and simulate the database in the memory. For example:CodeAs follows:

/*** Simulate the database in the memory and prepare test data. Calculate the bonus */public class tempdb {private tempdb () {}/ *** to record each person's monthly sales, only people are used, and no */public static Map <string, double> mapmonthsalemoney = new hashmap <string, double> (); static {// fill in the test data mapmonthsalemoney. put ("John", 10000.0); mapmonthsalemoney. put ("LEE Si", 20000.0); mapmonthsalemoney. put ("Wang Wu", 30000.0 );}}

 

(2) Calculate the bonus according to the bonus calculation rules. The sample code is as follows:

 

/*** Calculate the bonus object */public class prize {/*** calculates the bonus of a person for a certain period of time. Some parameters are not used in the demo, * However, it is used in actual business implementation to indicate that this is a specific business method, * therefore, these parameters are reserved. * @ Param User: the person who calculated the bonus * @ Param begin calculates the bonus start time * @ Param end calculates the bonus end time * @ return someone else in a certain */Public double calcprize (string user, date begin, date end) {double prize = 0.0; // calculates the business bonus for the current month. Everyone calculates prize = This. monthprize (user, begin, end); // calculates the cumulative bonus prize + = This. sumprize (user, begin, end); // you need to determine whether the employee is an ordinary employee or a business manager. Only the business manager has the team bonus if (this. ismanager (User) {prize + = This. groupprize (user, begin, end);} return Prize;}/*** calculates a person's business bonus for the current month. The parameters are repeated, no comments are added */private double monthprize (string user, date begin, date end) {// calculate the business bonus for the current month, and obtain the business amount for the current month by personnel, then multiply the value by 3% double prize = tempdb. mapmonthsalemoney. get (User) * 0.03; system. out. println (User + "current month Business Bonus" + Prize); Return Prize;}/*** calculates a person's accumulated bonus. The parameters are repeated, no longer comments */Public double sumprize (string user, date begin, date end) {// calculates the accumulated bonus. In fact, you should obtain the accumulated service amount by personnel, then multiply by 0.1% // for a simple demonstration, assuming that the total amount of business is 1000000 yuan double prize = 1000000*0.001; system. out. println (User + "accumulative bonus" + Prize); Return Prize ;} /*** determine whether a person is a common person or a business manager * @ Param User: The person judged * @ return true indicates that the person is a business manager, false indicates a common employee */private Boolean ismanager (string user) {// the employee's role should be retrieved from the database. // for demonstration, it is easy to judge, only Wang Wu is the manager if ("Wang Wu ". equals (User) {return true;} return false;}/*** calculate the team Business Award for the current month. If the parameters are repeated, the */Public double groupprize (string user, date begin, date end) {// calculate the Business Bonus of the team for the current month. Calculate the total business amount of the team and multiply it by 1%. // assume that the team's double group is 0.0; for (double D: tempdb. mapmonthsalemoney. values () {group + = D ;}double prize = Group * 0.01; system. out. println (User + "team business bonus for the current month" + Prize); Return prize ;}}

(3) Write a client to test whether the bonus can be calculated correctly. The sample code is as follows:

 
Public class client {public static void main (string [] ARGs) {// first create the calculation bonus object prize P = new prize (); // Date object is not used, therefore, if null is passed, double Zs = P. calcprize ("Zhang San", null, null); system. out. println ("=========== Zhang San deserves a bonus:" + ZS); double ls = P. calcprize ("Li Si", null, null); system. out. println ("=========== Li Si deserves a bonus:" + ls); double WW = P. calcprize ("Wang Wu", null, null); system. out. println ("=========== Manager Wang deserves a bonus:" + ww );}}

The test result is as follows:

 
Michael Jacob's business bonus for the current month is 300.0 Michael Jacob's cumulative bonus is 1000.0 ========== Michael Jacob deserves the bonus: 1300.0 Li Si's business bonus for the current month is 600.0 Li Si's accumulated bonus of 1000.0 ========== Li Si deserves the bonus: 1600.0 Wang Wu's business bonus for the current month: 900.0 Wang Wu's accumulated bonus: 1000.0 Wang Wu's team's business bonus for the current month: 600.0

 

1.4 What's the problem?

After reading the above implementation, it is quite simple, that is, the computing method is troublesome. Every rule must be implemented. Is it really easy? Do you have any questions?
For bonus calculation, the calculation method is complicated, but it is difficult to implement. It is better to solve the problem, but it is only useful. Program Set the existing Algorithm .
The most painful thing is that the calculation of these bonuses often changes, almost every quarter there will be small adjustments, there will be a large adjustment every year, this requires the implementation of software should be flexible enough, adjustments and modifications must be made quickly; otherwise, the actual business needs cannot be met.
To give a simple example, we need to add a "year-on-year growth bonus" based on business needs, that is, the sales volume in this month has increased from the previous month, and it has to reach a certain proportion, of course, the higher the growth ratio, the larger the bonus proportion. Then the software must implement such a function again and add it to the system correctly. After two months, the Business reward strategy has changed. You no longer need this bonus, or you have changed to a new one, then the software needs to remove this function from the software and then implement new functions.
How can we implement the above requirements?
Obviously, one solution is to extend functions through inheritance. The other solution is to add or delete new functions in the calculation bonus object, and when calculating the bonus, calling new functions or not calling some removed functions seriously violates the open-closed principle.
Another problem is that during the operation, different personnel participate in different bonus calculation methods. For example, if a business manager is involved in personal computing, also participate in the calculation of the team bonus, which means that you need to dynamically combine the part to be calculated during the running, that is, there will be a bunch of IF-Else.
To sum up, the bonus calculation faces the following problems:

    • (1) complicated computing Logic
    • (2) be flexible enough to conveniently add or reduce features
    • (3) A dynamic composite computing method is required, and different people participate in different computations.

The problem of bonus calculation described above is definitely not exaggerated. On the contrary, it has been simplified a lot and there are more troubles to be written. After all, our focus is on the design model rather than the business.
Abstract The above question. If there is a bonus calculation object, now we need to be able to flexibly add and reduce features to it, and also need to be able to dynamically combine features, each function is equivalent to calculating a certain part of the bonus.
The problem is:How can we add functions to an object transparently and implement dynamic combinations of functions?

 

 

 

To be continued

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.