<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Springboot中實(shí)現(xiàn)策略模式+工廠模式

          共 4172字,需瀏覽 9分鐘

           ·

          2021-05-23 08:41

          策略模式和工廠模式相信大家都比較熟悉,但是大家有沒有在springboot中實(shí)現(xiàn)策略和工廠模式?

          具體策略模式和工廠模式的UML我就不給出來了,使用這個(gè)這兩個(gè)模式主要是防止程序中出現(xiàn)大量的IF ELSE IF ELSE....。接下來咱們直接實(shí)現(xiàn),項(xiàng)目結(jié)構(gòu)圖:

          工廠類FactoryStrategy負(fù)責(zé)創(chuàng)建策略的工廠,代碼比較簡(jiǎn)單,比較關(guān)鍵的一點(diǎn)是AutoWired一個(gè)Map<String, Strategy> 這個(gè)會(huì)在初始化的時(shí)候?qū)⑺械腟trategy自動(dòng)加載到Map中,是不是很方便。使用concurrentHashMap是防止多線程操作的時(shí)候出現(xiàn)問題。同時(shí)還要注意@Service注解。

          package com.hqs.pattern.factory;

          import com.hqs.pattern.strategy.Strategy;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Service;

          import java.util.Map;
          import java.util.concurrent.ConcurrentHashMap;

          /**
           * @author huangqingshi
           * @Date 2019-01-31
           */

          @Service
          public class FactoryForStrategy {

              @Autowired
              Map<String, Strategy> strategys = new ConcurrentHashMap<>(3);

              public Strategy getStrategy(String component) throws Exception{
                  Strategy strategy = strategys.get(component);
                  if(strategy == null) {
                      throw new RuntimeException("no strategy defined");
                  }
                  return strategy;
              }

          }

          接下來就是Strategy接口,就一個(gè)doOperation方法。

          package com.hqs.pattern.strategy;

          /**
           * @author huangqingshi
           * @Date 2019-01-31
           */

          public interface Strategy {

              String doOperation();

          }

          定義接口的實(shí)現(xiàn),我定義了三個(gè), 都類似,這里我就放出一個(gè)來吧。Component里邊的one是指定其名字,這個(gè)會(huì)作為key放到Map strategys里邊。

          package com.hqs.pattern.strategy.impl;

          import com.hqs.pattern.strategy.Strategy;
          import org.springframework.stereotype.Component;

          /**
           * @author huangqingshi
           * @Date 2019-01-31
           */

          @Component("one")
          public class StrategyOne implements Strategy {
              @Override
              public String doOperation() {
                  return "one";
              }
          }

          好了,寫一個(gè)Controller類,用于進(jìn)行測(cè)試,當(dāng)然我還是使用swagger,使用swagger的時(shí)候有個(gè)細(xì)節(jié),就是注意生產(chǎn)上一定不能打開,否則是個(gè)非常可怕的事情。

          package com.hqs.pattern.controller;

          import com.hqs.pattern.factory.FactoryForStrategy;
          import org.springframework.beans.factory.annotation.Autowired;
          import org.springframework.stereotype.Controller;
          import org.springframework.web.bind.annotation.PostMapping;
          import org.springframework.web.bind.annotation.RequestParam;
          import org.springframework.web.bind.annotation.ResponseBody;

          /**
           * @author huangqingshi
           * @Date 2019-01-31
           */

          @Controller
          public class StrategyController {

              @Autowired
              FactoryForStrategy factoryForStrategy;

              @PostMapping("/strategy")
              @ResponseBody
              public String strategy(@RequestParam("key") String key) {
                  String result;
                  try {
                      result = factoryForStrategy.getStrategy(key).doOperation();
                  } catch (Exception e) {
                      result = e.getMessage();
                  }
                  return result;
              }


          }

          打開swagger進(jìn)行測(cè)試,輸入one,返回one。輸入four,返回no strategy defined。后續(xù)如果有新策略的話,直接實(shí)現(xiàn)即可。




          推薦閱讀:

          世界的真實(shí)格局分析,地球人類社會(huì)底層運(yùn)行原理

          企業(yè)IT技術(shù)架構(gòu)規(guī)劃方案

          論數(shù)字化轉(zhuǎn)型——轉(zhuǎn)什么,如何轉(zhuǎn)?

          企業(yè)10大管理流程圖,數(shù)字化轉(zhuǎn)型從業(yè)者必備!

          【中臺(tái)實(shí)踐】華為大數(shù)據(jù)中臺(tái)架構(gòu)分享.pdf

          數(shù)字化轉(zhuǎn)型的本質(zhì)(10個(gè)關(guān)鍵詞)

          小米用戶畫像實(shí)戰(zhàn),48頁P(yáng)PT下載

          華為大數(shù)據(jù)解決方案(PPT)

          瀏覽 64
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  精品一二三免费区 | 国产骚逼被操 | 国产亚洲 久一区二区写真 | 亚洲超级高清无码第一在线视频观看 | 爱搞搞就搞搞爱 |