Big talk Design Model Study Notes

Source: Internet
Author: User

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

 

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.