<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                * * [TOC] | | | | --- | --- | | | | [TOC] # 策略模式(Strategy Pattern) 策略模式又叫政策模式(Policy Pattern),它是將定義的算法家族分別封裝起來,讓它們之間可以互相替換,從而讓算法的變化不會影響到使用算法的用戶。可以避免多重分支的if......else和switch語句。 策略模式的主要角色如下: * 抽象策略(Strategy)類:這是一個抽象角色,通常由一個接口或抽象類實現。此角色給出所有的具體策略類所需的接口。 * 具體策略(Concrete Strategy)類:實現了抽象策略定義的接口,提供具體的算法實現或行為。 * 環境(Context)類:持有一個策略類的引用,最終給客戶端調用。 ![](https://img.kancloud.cn/ff/2b/ff2bbdb86e17de13d5017224b972faed_1647x581.png) ## 1 普通案例(會員卡打折) ``` // 會員卡接口 public interface VipCard { public void discount(); } public class GoldCard implements VipCard { @Override public void discount() { System.out.println("金卡打7折"); } } public class SilverCard implements VipCard { @Override public void discount() { System.out.println("銀卡打8折"); } } public class CopperCard implements VipCard { @Override public void discount() { System.out.println("銅卡打9折"); } } public class Normal implements VipCard { @Override public void discount() { System.out.println("普通會員沒有折扣"); } } // 會員卡容器類 public class VipCardFactory { private static Map<String, VipCard> map = new ConcurrentHashMap<>(); static { map.put("gold", new GoldCard()); map.put("silver", new SilverCard()); map.put("copper", new CopperCard()); } public static VipCard getVIPCard(String level) { return map.get(level) != null ? map.get(level) : new Normal(); } } // 測試方法 public static void main(String[] args) { //金卡打7折 VipCardFactory.getVIPCard("gold").discount(); //銀卡打8折 VipCardFactory.getVIPCard("silver").discount(); //普通會員沒有折扣 VipCardFactory.getVIPCard("other").discount(); } ``` 用一個容器(Map)裝起來,可以通過傳進來的參數直接獲取對應的策略,避免了if...else。 ## 2. 總結 適用場景: * 系統中有很多類,而它們的區別僅僅在于它們的行為不同。 * 系統需要動態地在幾種算法中選擇一種。 * 需要屏蔽算法規則。 優點: * 符合開閉原則。 * 避免使用多重條件語句。 * 可以提高算法的保密性和安全性。 * 易于擴展。 缺點: * 客戶端必須知道所有的策略,并且自行決定使用哪一個策略類。 * 代碼中會產生非常多的策略類,增加維護難度。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看