Practical questions:
Because the supermarket go out to do promotional activities, but each promotion is not the same way, so need to develop a software, the salesperson just enter the original price and then select the activity type, you can calculate the price after the discount.
Ordinary people's approach:
mian () {
String input = ReadLine ();
double price = ReadLine ();
switch (input)
case "95 percent":
Price = Price * 0.95;
break;
case "Full 100 rebate":
if (price >=100)
price = PRICE-50;
break;
case "no discount":
break;
println ("Price After Discount:" +price);
}
There are a few problems with writing this:
1. If the new discount method is added now, the code should be modified, which will break the "open-closed" principle.
2. This code is not easy to reuse. When the other parts of the cashier system also need to calculate the price of the discount, you can only copy the code in the past, if in the maintenance of the developer found "full 100 return 50" written "full 10000 back to 50", this time will need to change two places, very inconvenient.
So the high-level approach is:
1. Abstract this discount into classes, so as to facilitate the reuse of code;
2. Transfer the judgment of the different discounts from the client program to the called class, thereby reducing the complexity of the code in the client program.
Class diagrams for high-level practices:
1. At this point, the judgment of the promotion type is transferred from the client to the constructor of the context class, so that the user program only needs to pass a promotion name to the context's constructor, and the private member variable "promotional subclass object" In the context class will be able to obtain the object of the promotion class.
2. After the code is constructed, to get the discounted price, you only need:
Double price = new Context ("Full 100 Rebate"). Contextinterface (521);
Comparison of policy mode and simple Factory mode:
1. Factory mode factory is only responsible for the production of objects, it returned the object to the client after the wash-off; Your client love to the object why do it;
The context class in the policy mode is similar to the factory class in Factory mode, but there is an essential difference:
the context class's constructor (key) also has the ability to create different objects based on key, and in addition, there is a function contextinterface in the context class that can invoke the common function of the policy class. So the client only needs a context class to be able to do two things: 1. Create a policy class object, 2. Call a common function in the policy class object. Therefore, when using the policy mode, users do not need to know the existence of the policy classes, the user only need to know a context class and each policy class corresponding to the key, you can perform the functions of different policies.
Policy classes in the policy mode are premised on:
Each policy class must have a public interface, and each policy class implements the abstract function of the public interface, so that the Contextinterface () function in the context can invoke the public function.
2. Factories in Factory mode can create objects of two types of classes:
A) A class it needs to create several kinds of objects, each of which has different property values;
b) A class it needs to create several kinds of objects, each of which is a subclass of a parent class/interface;
The policy class created by the policy mode can only be the second one, and all subclasses must have a common function so that the contextinterface () Call of the context class.
The role of the policy model:
1. After using the policy mode, if you need to add a policy, you just need to create a subclass that implements the strategy interface, override the functions in it, and then add this subclass's judgment in the context constructor. The code for the client, Contextinterface () in the context, does not need to be modified.
2. Similarities between the strategy model and the Simple factory model:
The context's constructor, factory's Getbean () are used to determine what type of object is created.
Both the Factory mode and the policy model can transfer the "client to what type of judgment" to factory/context, thereby reducing the complexity of the client code and reducing the coupling between the server and the client.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Three minutes to understand "strategy mode"--easy to master in design mode