It’s the weekend again, the Nuggets said, summing up their notes. The workplace is a battlefield. If a person lacks the awareness to plan ahead, is always undermotivated or headstrong, his situation can be dangerous.
case
Go straight to the code! Demand, order source: PC terminal and Mobile terminal respectively, payment methods: Ali Pay, wechat Pay, UnionPay. So, if-else quickly respond to demand, in order to complete tasks, or in order to get home from work…
public static final String PC = "pc";
public static final String MOBILE = "mobile";
public static final String ALI_PAY = "aliPay";
public static final String WE_CHAT = "weChat";
/** * Demand, order source: PC terminal and Mobile terminal respectively, payment method: Ali Pay, wechat Pay, UnionPay. * So, if-else quickly respond to demands, for tasks, or for getting home from work... * *@param order
*/
@Deprecated
@Override
public void orderServiceOld(Order order) {
log.info("Old if-else");
if (PC.equals(order.getSource())) {
log.info("Logic for processing PC orders");
// The logic for processing orders on the PC side
if(ALI_PAY.equals(order.getPayMethod())){
// Process the logic of alipay orders
} else if (WE_CHAT.equals(order.getPayMethod())){
// The logic of processing wechat pay orders}}else if (MOBILE.equals(order.getSource())) {
log.info("Logic for processing MOBILE orders");
// Handle the logic of MOBILE orders
if(ALI_PAY.equals(order.getPayMethod())){
// Process the logic of alipay orders
} else if (WE_CHAT.equals(order.getPayMethod())){
// The logic of processing wechat pay orders}}else {
log.info("Other logic");
// Other logic}}Copy the code
strategy
In the Strategy Pattern, the behavior of a class or its algorithm can be changed at run time. This type of design pattern is behavioral. In the policy pattern, we create objects that represent various policies and a context object whose behavior changes as the policy object changes. The policy object changes the execution algorithm of the context object.
Use policy mode instead of if-else. Policy patterns are implemented similarly. The main idea is to define uniform behavior (interfaces or abstract classes) and implement logical processing (corresponding implementation classes) under different policies. The client uses its own processing class, engineering or otherwise.
Advantages: 1. The algorithm can be switched freely. 2. Avoid using multiple conditional judgments. 3. Good expansibility.
Disadvantages: 1. Policy classes will increase. 2. All policy classes need to be exposed.
** If a system has more than four policies, consider using mixed mode to solve the problem of policy class inflation.
annotations
Order processing as an example, first summarize the structure:
-
There is an Order entity class Order;
-
First, define an OrderHandler interface that specifies how the order is processed.
-
Define an OrderHandlerType annotation that represents an order from something like what source it is used to process;
-
OrderHandlerType = OrderHandlerType = OrderHandlerType = OrderHandlerType MobileAliPayOrderHandler, MobileWeChatOrderHandler, PcAliPayOrderHandler, PcWeChatOrderHandler;
-
Once you’re ready, you want to inject various order processing handlers into the Spring container and, in the method, use the policy (order source) to determine which OrderHandler to select to process the order.
-
Call the order interface OrderService and get the corresponding handler by order origin and payment method;
conclusion
As a professional, only keep a clear head, learn to evaluate the situation, so that they have competitiveness, in order to stand firm in the workplace.
reference
Don’t use if-else, use annotations instead
The strategy pattern