Design Mode-Rule Mode 1

Source: Internet
Author: User

* Hypothesis:

* You find that you want to design a software with a cashier discount for a store.

* How can we design the best?

* We should consider the expansion of future functions and the discount ratio of some commodities.

*

* How can I get a discount first?

* For example, there are only three types of store customers: ordinary users, platinum users, and diamond users (vvvip may be available in the future)

* Different customer identities have different discounts.

*

* Obviously, writing in a class won't work. PS: A single responsibility

Therefore, we would like to regard each situation as a separate class, but in order to unify their behavior, we also need a public method (which can be implemented using an interface or an abstract class)

This is basically in line with the Policy mode:

Defines a series of algorithms, encapsulates them one by one, and enables them to replace each other. The algorithms in this mode can change independently from the client.

PS: But for this discount system, this is not the best result, because you still need to go to the new object on the client, which is not logical, suppose I am running this program,

If there are already new platinum customers in Main, this client can only use platinum customers' discounts.

If only new diamonds are available, they can only be discounted.

Maybe you think I can use this code ....

PS: Kiss ~, You know that if this program will be handed over to the salesperson in the future, they also need to know the Java language to modify, compile ,,,,!

Maybe you want to add if-ekse in main

PS: Well, this is a good solution, but we still have a better solution-factory approach .... The first few of the big talk design patterns have their solutions. If you are interested, please leave a message to discuss them.

Design Mode-Rule Mode 1

Related Article

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.