In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "case analysis of Java strategy pattern". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "case analysis of Java strategy pattern".
Advantages
1. The algorithm can be switched freely.
two。 Avoid using multiple conditional judgments.
3. It has good scalability.
Shortcoming
1. Policy classes will increase.
two。 All policy classes need to be exposed.
Working with scen
1. If there are many classes in a system, and the only difference between them is their behavior, then using the policy pattern can dynamically make an object choose one behavior among many behaviors.
two。 A system needs to choose one of several algorithms dynamically.
3. If an object has a lot of behavior, if the appropriate pattern is not used, these behaviors have to be implemented using multiple conditional selection statements.
I. the way of realization
Suppose a scene, we in the e-commerce system, orders are divided into many kinds, such as: ordinary orders, second-kill orders, group purchase orders and so on. When we need to create an order, because the type of order is different, we need to execute different business logic according to the type of order.
1. Order type enumeration class package com.asurplus.common.strategy;import lombok.AllArgsConstructor;import lombok.Getter;/** * order type enumeration class * / @ Getter@AllArgsConstructorpublic enum OrderTypeEnum {COMMON (1001, "ordinary order"), SECKILL (1002, "kill order"), SPELL (1003, "group purchase order"); int type; String desc;}
Our orders are divided into three types: ordinary orders, instant orders and group orders.
2. Order processing interface package com.asurplus.common.strategy;/** * order processing interface * / public interface OrderService {/ * create order * * @ return * / void createOrder (); / * * get order type * * @ return * / OrderTypeEnum type ();} 3. General order processor package com.asurplus.common.strategy Import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service;/** * General order processor * / @ Slf4j@Servicepublic class CommonOrderServiceImpl implements OrderService {@ Override public void createOrder () {log.info ("create a normal order");} @ Override public OrderTypeEnum type () {return OrderTypeEnum.COMMON;}} 4, package com.asurplus.common.strategy;import lombok.extern.slf4j.Slf4j, order processor Import org.springframework.stereotype.Service;/** * second kill order processor * / @ Slf4j@Servicepublic class SeckillOrderServiceImpl implements OrderService {@ Override public void createOrder () {log.info ("create a second kill order");} @ Override public OrderTypeEnum type () {return OrderTypeEnum.SECKILL;}} 5, group purchase order processor package com.asurplus.common.strategy;import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Service / * * Group order processor * / @ Slf4j@Servicepublic class SpellOrderServiceImpl implements OrderService {@ Override public void createOrder () {log.info ("create Group order");} @ Override public OrderTypeEnum type () {return OrderTypeEnum.SPELL;}} 6, order Manager package com.asurplus.common.strategy;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import java.util.List The power of import java.util.Optional;/** * order Manager * / @ Componentpublic class OrderManager {/ * Autowired annotations * / @ Autowired private List orderServices / * create order * * @ param type order type * @ return * / public void createOrder (int type) {/ * find the corresponding processor * / Optional any = orderServices.stream (). Filter (f-> f.type (). GetType () = = type). FindAny () / * there is no corresponding processor * / if (! any.isPresent ()) {throw new RuntimeException ("No corresponding order implementation found");} / / create order any.get (). CreateOrder ();}}
This shows the power of @ Autowired, which can automatically inject multiple objects at once. According to the order type, the corresponding processor is selected to process the order.
2. Test 1. Introduce dependency org.springframework.boot spring-boot-starter-test2, test case package com.asurplus.common.strategy;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;/** * policy mode * / @ RunWith (SpringRunner.class) @ SpringBootTestpublic class TestMain {@ Autowired private OrderManager orderManager @ Test public void test () {/ / create a kill order orderManager.createOrder (OrderTypeEnum.SECKILL.getType ());}}
Output result
Thank you for your reading, the above is the content of "case Analysis of Java Strategy pattern". After the study of this article, I believe you have a deeper understanding of the case analysis of Java strategy pattern, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.