算法族的集中管理——策略模式
生活随笔
收集整理的這篇文章主要介紹了
算法族的集中管理——策略模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
策略模式是指對一系列的算法定義,并將每一個算法封裝起來,而且使它們還可以相互替換。策略模式讓算法獨立于使用它的客戶而獨立變化。
我們正好可以使用策略模式加適配器模式來將查找算法的調用重新架構。
分角色
應用
為了學習策略模式,我們將查找算法中的BST和RedBlackBST兩個類作為研究素材,研究設計模式肯定是不能修改原有代碼,也就是說BST和RedBlackBST不會做任何修改,在此基礎上,我們使用適配器模式將其封裝取出
- BSTAdapter
- RedBlackBSTAdapter
下面展示BSTAdapter的代碼,RedBlackAdapter與它相似。
package pattern.strategy;import algorithms.search.ST; import algorithms.search.STImpl.BST;public class BSTAdapter extends Strategy {@Overridepublic void algorithm() {logger.info(this.getClass().getName());ST<Integer, String> st;st = new ST<Integer, String>(new BST<Integer, String>());testST(st);}}然后貼上Strategy的代碼,省略了一些與模式無關的內容。
public abstract class Strategy {protected static final Logger logger = LogManager.getLogger();public abstract void algorithm();// 核心抽象方法protected void testST(ST<Integer, String> sst) {...} }最后是Context的代碼
package pattern.strategy;public class Context {private Strategy strategy;public void setStrategy(Strategy strategy) {this.strategy = strategy;}public void testSTStrategy() {strategy.algorithm();} }客戶端的調用方法
package pattern.strategy;import org.junit.Test;public class StrategyClient {@Testpublic void testStrategy() {Context context = new Context();context.setStrategy(new RedBlackBSTAdapter());// 運行時指定具體類型context.testSTStrategy();context.setStrategy(new BSTAdapter());// 運行時指定具體類型context.testSTStrategy();} }輸出:
11:22:50[algorithm]: pattern.strategy.RedBlackBSTAdapter 11:22:50[testST]: 總耗時:69ms 11:22:50[algorithm]: pattern.strategy.BSTAdapter 11:22:51[testST]: 總耗時:88ms下面給出類圖
策略模式總結
缺點:
以上展示的策略模式仍舊處于初級階段,具體算法均需要通過繼承來實現,可以作為研究學習使用,仍然存在一些問題,例如對具體策略類的管理,如果使用場景不當,可能每次都要新建一個具體策略類,因此我們這里將其與適配器模式聯用有效地避免了這一點。
轉載于:https://www.cnblogs.com/Evsward/p/Strategy.html
總結
以上是生活随笔為你收集整理的算法族的集中管理——策略模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 剑指offer(19)顺时针打印矩阵
- 下一篇: 初始民大-印象篇