您好,登錄后才能下訂單哦!
這篇文章主要講解了“Java策略模式實例分析”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Java策略模式實例分析”吧!
1.算法可以自由切換。
2.避免使用多重條件判斷。
3.擴展性良好。
1.策略類會增多。
2.所有策略類都需要對外暴露。
1.如果在一個系統(tǒng)里面有許多類,它們之間的區(qū)別僅在于它們的行為,那么使用策略模式可以動態(tài)地讓一個對象在許多行為中選擇一種行為。
2.一個系統(tǒng)需要動態(tài)地在幾種算法中選擇一種。
3.如果一個對象有很多的行為,如果不用恰當(dāng)?shù)哪J?,這些行為就只好使用多重的條件選擇語句來實現(xiàn)。
假設(shè)一個場景,我們在電商系統(tǒng)中,訂單分為很多種,例如:普通訂單,秒殺訂單,拼團訂單等等。我們需要創(chuàng)建一個訂單的時候,由于訂單的類型不同,我們需要根據(jù)訂單的類型執(zhí)行不同的業(yè)務(wù)邏輯。
package com.asurplus.common.strategy; import lombok.AllArgsConstructor; import lombok.Getter; /** * 訂單類型枚舉類 */ @Getter @AllArgsConstructor public enum OrderTypeEnum { COMMON(1001, "普通訂單"), SECKILL(1002, "秒殺訂單"), SPELL(1003, "拼團訂單"); int type; String desc; }
我們的訂單分為三種,普通訂單,秒殺訂單,拼團訂單。
package com.asurplus.common.strategy; /** * 訂單處理接口 */ public interface OrderService { /** * 創(chuàng)建訂單 * * @return */ void createOrder(); /** * 獲取訂單類型 * * @return */ OrderTypeEnum type(); }
package com.asurplus.common.strategy; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * 普通訂單處理器 */ @Slf4j @Service public class CommonOrderServiceImpl implements OrderService { @Override public void createOrder() { log.info("創(chuàng)建 普通訂單"); } @Override public OrderTypeEnum type() { return OrderTypeEnum.COMMON; } }
package com.asurplus.common.strategy; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * 秒殺訂單處理器 */ @Slf4j @Service public class SeckillOrderServiceImpl implements OrderService { @Override public void createOrder() { log.info("創(chuàng)建 秒殺訂單"); } @Override public OrderTypeEnum type() { return OrderTypeEnum.SECKILL; } }
package com.asurplus.common.strategy; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; /** * 拼團訂單處理器 */ @Slf4j @Service public class SpellOrderServiceImpl implements OrderService { @Override public void createOrder() { log.info("創(chuàng)建 拼團訂單"); } @Override public OrderTypeEnum type() { return OrderTypeEnum.SPELL; } }
package com.asurplus.common.strategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.Optional; /** * 訂單管理器 */ @Component public class OrderManager { /** * Autowired 注解的強大之處 */ @Autowired private List<OrderService> orderServices; /** * 創(chuàng)建訂單 * * @param type 訂單類型 * @return */ public void createOrder(int type) { /** * 根據(jù)訂單類型,找到對應(yīng)的處理器 */ Optional<OrderService> any = orderServices.stream().filter(f -> f.type().getType() == type).findAny(); /** * 沒有對應(yīng)的處理器 */ if (!any.isPresent()) { throw new RuntimeException("沒有找到相應(yīng)的訂單實現(xiàn)"); } // 創(chuàng)建訂單 any.get().createOrder(); } }
這里就能體現(xiàn)出 @Autowired 的強大之處,可以一次性自動注入多個對象。根據(jù)訂單類型,選出對應(yīng)的處理器來處理該訂單。
<!-- 測試依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
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; /** * 策略模式 */ @RunWith(SpringRunner.class) @SpringBootTest public class TestMain { @Autowired private OrderManager orderManager; @Test public void test() { // 創(chuàng)建 秒殺訂單 orderManager.createOrder(OrderTypeEnum.SECKILL.getType()); } }
輸出結(jié)果
感謝各位的閱讀,以上就是“Java策略模式實例分析”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Java策略模式實例分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。