A small cash register system for a mall. At the beginning of programming, I did not thinkCodeThe maintainability.ProgramIt can only be a normal charge. What should I do when the mall is planning to launch promotions? I had to rewrite the program. The maintainability of the purchase agent should be considered when you write this program again.
We should consider that no matter how the mall engages in promotions, the difference is only the discount processing method at the cashier's day, while the basic total price calculation method is the same, and different discount methods are characteristic. Therefore, we can write an abstract class in which there is an abstract method (this abstract method uses the current total price as the input parameter and outputs the discounted price ), then we will write various discount methods and classes that inherit from the previous abstract class and rewrite the Discount calculation method based on our discount method. Then, write a cash receiving factory and define a reference using the parent class. When different discount methods are selected, use different subclass to instantiate the reference, do not return the instantiated object.
The requirement must be changed! Therefore, developers should consider how to make their programs better adapt to changes, rather than complaining about the unreasonable nature of the customer. The customer will not care about the sweat of programmers when they work overtime, nor will they trust the tears of programmers when they are unemployed.
In the above example, because the mall may frequently change the discount quota and sample quota, it is really bad to re-compile and deploy the code each time.AlgorithmThere should be a better way. Make a good research on the design mode. In-depth introduction to design patterns