Related content: object-oriented ideas brainstorming (1)
AssociatedArticleA specific case of the open and closed principle was discussed. The first two of them are super complicated designs and completely do not comply with the open and closed principle. However, I think if I have never learned the so-called design patterns, I will never make the first two mistakes of "over-pattern. Therefore, the fans of design patterns should be careful to let youCodeWhat gets disgusting is often what you are keen on.
Then, use the third delegate.
The fourth method is the combination interface.
Maybe the example is not good enough. I think the third and fourth are not perfect.
Let's review the design requirements:"
Three types of books need to be processed, such as digital and consumption. The tax rate of products needs to be calculated. The tax rate of books is 0.1 of the price, and that of digital and consumer products is 0.11 of the price. Information about the three types of products needs to be obtained, the information of books and consumer products is: "name:" + name;. The information of digital products is: "Digital name:" + name;
"
There is a product category, and then let you join the tax rate and print the problem, what do you do?
You can select the appropriate policy for each product, as shown in the design adopted by the associated articles.
Product A. Tax Rate = Policy 1.
Product B. Tax Rate = Policy 2.
However:
Product c. Tax Rate = Policy 1.
Product D. Tax Rate = Policy 2.
......
You may need to produce one thousand different products, but find that both strategies come and go.
My analysis is like this. First of all, there is a product class. Now we need to add and print. According to the requirements, policies depend not on specific products, but on the product type. Therefore, I think we should first increase the abstraction to the previous level and program the types of different products.
Digital category {tax rate}
Product Category {type A; Price B; Name C;
Total price: A. Tax Rate * B;
Print: A. Print (c)
}
Product Type: computer. A = digital.