{"id":112,"date":"2008-03-08T13:42:13","date_gmt":"2008-03-08T13:42:13","guid":{"rendered":""},"modified":"2008-03-08T13:42:13","modified_gmt":"2008-03-08T13:42:13","slug":"112","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=112","title":{"rendered":"\u7b56\u7565\u6a21\u5f0f(Strategy Pattern)"},"content":{"rendered":"<p>\u4e00\u3001 \u7b56\u7565\uff08Strategy\uff09\u6a21\u5f0f<br \/>\u7b56\u7565\u6a21\u5f0f\u7684\u7528\u610f\u662f\u9488\u5bf9\u4e00\u7ec4\u7b97\u6cd5\uff0c\u5c06\u6bcf\u4e00\u4e2a\u7b97\u6cd5\u5c01\u88c5\u5230\u5177\u6709\u5171\u540c\u63a5\u53e3\u7684\u72ec\u7acb\u7684\u7c7b\u4e2d\uff0c\u4ece\u800c\u4f7f\u5f97\u5b83\u4eec\u53ef\u4ee5\u76f8\u4e92\u66ff\u6362\u3002\u7b56\u7565\u6a21\u5f0f\u4f7f\u5f97\u7b97\u6cd5\u53ef\u4ee5\u5728\u4e0d\u5f71\u54cd\u5230\u5ba2\u6237\u7aef\u7684\u60c5\u51b5\u4e0b\u53d1\u751f\u53d8\u5316\u3002<\/p>\n<p>\u5047\u8bbe\u73b0\u5728\u8981\u8bbe\u8ba1\u4e00\u4e2a\u8d29\u5356\u5404\u7c7b\u4e66\u7c4d\u7684\u7535\u5b50\u5546\u52a1\u7f51\u7ad9\u7684\u8d2d\u7269\u8f66\uff08Shopping Cat\uff09\u7cfb\u7edf\u3002\u4e00\u4e2a\u6700\u7b80\u5355\u7684\u60c5\u51b5\u5c31\u662f\u628a\u6240\u6709\u8d27\u54c1\u7684\u5355\u4ef7\u4e58\u4e0a\u6570\u91cf\uff0c\u4f46\u662f\u5b9e\u9645\u60c5\u51b5\u80af\u5b9a\u6bd4\u8fd9\u8981\u590d\u6742\u3002\u6bd4\u5982\uff0c\u672c\u7f51\u7ad9\u53ef\u80fd\u5bf9\u6240\u6709\u7684\u6559\u6750\u7c7b\u56fe\u4e66\u5b9e\u884c\u6bcf\u672c\u4e00\u5143\u7684\u6298\u6263\uff1b\u5bf9\u8fde\u73af\u753b\u7c7b\u56fe\u4e66\u63d0\u4f9b\u6bcf\u672c7\uff05\u7684\u4fc3\u9500\u6298\u6263\uff0c\u800c\u5bf9\u975e\u6559\u6750\u7c7b\u7684\u8ba1\u7b97\u673a\u56fe\u4e66\u67093\uff05\u7684\u6298\u6263\uff1b\u5bf9\u5176\u4f59\u7684\u56fe\u4e66\u6ca1\u6709\u6298\u6263\u3002\u7531\u4e8e\u6709\u8fd9\u6837\u590d\u6742\u7684\u6298\u6263\u7b97\u6cd5\uff0c\u4f7f\u5f97\u4ef7\u683c\u8ba1\u7b97\u95ee\u9898\u9700\u8981\u7cfb\u7edf\u5730\u89e3\u51b3\u3002<\/p>\n<p>\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u53ef\u4ee5\u628a\u884c\u4e3a\u548c\u73af\u5883\u5206\u5272\u5f00\u6765\u3002\u73af\u5883\u7c7b\u8d1f\u8d23\u7ef4\u6301\u548c\u67e5\u8be2\u884c\u4e3a\u7c7b\uff0c\u5404\u79cd\u7b97\u6cd5\u5219\u5728\u5177\u4f53\u7b56\u7565\u7c7b\uff08ConcreteStrategy\uff09\u4e2d\u63d0\u4f9b\u3002\u7531\u4e8e\u7b97\u6cd5\u548c\u73af\u5883\u72ec\u7acb\u5f00\u6765\uff0c\u7b97\u6cd5\u7684\u589e\u51cf\u3001\u4fee\u6539\u90fd\u4e0d\u4f1a\u5f71\u54cd\u73af\u5883\u548c\u5ba2\u6237\u7aef\u3002\u5f53\u51fa\u73b0\u65b0\u7684\u4fc3\u9500\u6298\u6263\u6216\u73b0\u6709\u7684\u6298\u6263\u653f\u7b56\u51fa\u73b0\u53d8\u5316\u65f6\uff0c\u53ea\u9700\u8981\u5b9e\u73b0\u65b0\u7684\u7b56\u7565\u7c7b\uff0c\u5e76\u5728\u5ba2\u6237\u7aef\u767b\u8bb0\u5373\u53ef\u3002\u7b56\u7565\u6a21\u5f0f\u76f8\u5f53\u4e8e&#8221;\u53ef\u63d2\u5165\u5f0f\uff08Pluggable\uff09\u7684\u7b97\u6cd5&#8221;\u3002<\/p>\n<p>\u4e8c\u3001 \u7b56\u7565\u6a21\u5f0f\u7684\u7ed3\u6784<br \/>\u7b56\u7565\u6a21\u5f0f\u662f\u5bf9\u7b97\u6cd5\u7684\u5305\u88c5\uff0c\u662f\u628a\u4f7f\u7528\u7b97\u6cd5\u7684\u8d23\u4efb\u548c\u7b97\u6cd5\u672c\u8eab\u5206\u5272\u5f00\uff0c\u59d4\u6d3e\u7ed9\u4e0d\u540c\u7684\u5bf9\u8c61\u7ba1\u7406\u3002\u7b56\u7565\u6a21\u5f0f\u901a\u5e38\u628a\u4e00\u4e2a\u7cfb\u5217\u7684\u7b97\u6cd5\u5305\u88c5\u5230\u4e00\u7cfb\u5217\u7684\u7b56\u7565\u7c7b\u91cc\u9762\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u62bd\u8c61\u7b56\u7565\u7c7b\u7684\u5b50\u7c7b\u3002\u7528\u4e00\u53e5\u8bdd\u6765\u8bf4\uff0c\u5c31\u662f\uff1a&#8221;\u51c6\u5907\u4e00\u7ec4\u7b97\u6cd5\uff0c\u5e76\u5c06\u6bcf\u4e00\u4e2a\u7b97\u6cd5\u5c01\u88c5\u8d77\u6765\uff0c\u4f7f\u5f97\u5b83\u4eec\u53ef\u4ee5\u4e92\u6362\u3002&#8221;<\/p>\n<p>\u7b56\u7565\u53c8\u79f0\u505a\u653f\u7b56\uff08Policy\uff09\u6a21\u5f0f\u3010GOF95\u3011\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u610f\u6027\u7684\u7b56\u7565\u6a21\u5f0f\u7ed3\u6784\u56fe\uff1a<br \/><img decoding=\"async\" src=\"http:\/\/www.poloo.org\/wp-content\/attachments\/month_0803\/p200838134132.gif\" \/><br \/>\u8fd9\u4e2a\u6a21\u5f0f\u6d89\u53ca\u5230\u4e09\u4e2a\u89d2\u8272\uff1a<\/p>\n<p>\u73af\u5883\uff08Context\uff09\u89d2\u8272\uff1a\u6301\u6709\u4e00\u4e2aStrategy\u7c7b\u7684\u5f15\u7528\u3002 <br \/>\u62bd\u8c61\u7b56\u7565\uff08Strategy\uff09\u89d2\u8272\uff1a\u8fd9\u662f\u4e00\u4e2a\u62bd\u8c61\u89d2\u8272\uff0c\u901a\u5e38\u7531\u4e00\u4e2a\u63a5\u53e3\u6216\u62bd\u8c61\u7c7b\u5b9e\u73b0\u3002\u6b64\u89d2\u8272\u7ed9\u51fa\u6240\u6709\u7684\u5177\u4f53\u7b56\u7565\u7c7b\u6240\u9700\u7684\u63a5\u53e3\u3002 <br \/>\u5177\u4f53\u7b56\u7565\uff08ConcreteStrategy\uff09\u89d2\u8272\uff1a\u5305\u88c5\u4e86\u76f8\u5173\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002 <\/p>\n<p>\u4e09\u3001 \u793a\u610f\u6027\u6e90\u4ee3\u7801<\/p>\n<p><code>\/\/ Strategy pattern -- Structural example&nbsp;&nbsp;<br \/>using System;<\/p>\n<p>\/\/ \"Strategy\"<br \/>abstract class Strategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public void AlgorithmInterface();<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategyA\"<br \/>class ConcreteStrategyA : Strategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void AlgorithmInterface()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"Called ConcreteStrategyA.AlgorithmInterface()\");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategyB\"<br \/>class ConcreteStrategyB : Strategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void AlgorithmInterface()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"Called ConcreteStrategyB.AlgorithmInterface()\");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategyC\"<br \/>class ConcreteStrategyC : Strategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;override public void AlgorithmInterface()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"Called ConcreteStrategyC.AlgorithmInterface()\");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"Context\"<br \/>class Context<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;Strategy strategy;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public Context( Strategy strategy )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.strategy = strategy;<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public void ContextInterface()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;strategy.AlgorithmInterface();<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/ Client test<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public class Client<br \/>{<br \/>&nbsp;&nbsp;public static void Main( string[] args )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Three contexts following different strategies<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Context c = new Context( new ConcreteStrategyA() );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;c.ContextInterface();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Context d = new Context( new ConcreteStrategyB() );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;d.ContextInterface();<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;Context e = new Context( new ConcreteStrategyC() );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;e.ContextInterface();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u56db\u3001 \u4f55\u65f6\u4f7f\u7528\u4f55\u79cd\u5177\u4f53\u7b56\u7565\u89d2\u8272<br \/>\u5728\u5b66\u4e60\u7b56\u7565\u6a21\u5f0f\u65f6\uff0c\u5b66\u5458\u5e38\u95ee\u7684\u4e00\u4e2a\u95ee\u9898\u662f\uff1a\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u4ece\u7b56\u7565\u6a21\u5f0f\u4e2d\u770b\u51fa\u54ea\u4e00\u4e2a\u5177\u4f53\u7b56\u7565\u9002\u7528\u4e8e\u54ea\u4e00\u79cd\u60c5\u51b5\u5462\uff1f<\/p>\n<p>\u7b54\u6848\u975e\u5e38\u7b80\u5355\uff0c\u7b56\u7565\u6a21\u5f0f\u5e76\u4e0d\u8d1f\u8d23\u505a\u8fd9\u4e2a\u51b3\u5b9a\u3002\u6362\u8a00\u4e4b\uff0c\u5e94\u5f53\u7531\u5ba2\u6237\u7aef\u81ea\u5df1\u51b3\u5b9a\u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u4f7f\u7528\u4ec0\u4e48\u5177\u4f53\u7b56\u7565\u89d2\u8272\u3002\u7b56\u7565\u6a21\u5f0f\u4ec5\u4ec5\u5c01\u88c5\u7b97\u6cd5\uff0c\u63d0\u4f9b\u65b0\u7b97\u6cd5\u63d2\u5165\u5230\u5df2\u6709\u7cfb\u7edf\u4e2d\uff0c\u4ee5\u53ca\u8001\u7b97\u6cd5\u4ece\u7cfb\u7edf\u4e2d&#8221;\u9000\u4f11&#8221;\u7684\u65b9\u4fbf\uff0c\u7b56\u7565\u6a21\u5f0f\u5e76\u4e0d\u51b3\u5b9a\u5728\u4f55\u65f6\u4f7f\u7528\u4f55\u79cd\u7b97\u6cd5\u3002<\/p>\n<p>\u4e94\u3001 \u4e00\u4e2a\u5b9e\u9645\u5e94\u7528\u7b56\u7565\u6a21\u5f0f\u7684\u4f8b\u5b50<br \/>\u4e0b\u9762\u7684\u4f8b\u5b50\u5229\u7528\u7b56\u7565\u6a21\u5f0f\u5728\u6392\u5e8f\u5bf9\u8c61\u4e2d\u5c01\u88c5\u4e86\u4e0d\u540c\u7684\u6392\u5e8f\u7b97\u6cd5\uff0c\u8fd9\u6837\u4ee5\u4fbf\u5141\u8bb8\u5ba2\u6237\u7aef\u52a8\u6001\u7684\u66ff\u6362\u6392\u5e8f\u7b56\u7565\uff08\u5305\u62ecQuicksort\u3001Shellsort\u548cMergesort\uff09\u3002<\/p>\n<p><code>\/\/ Strategy pattern -- Real World example&nbsp;&nbsp;<br \/>using System;<br \/>using System.Collections;<\/p>\n<p>\/\/ \"Strategy\"<br \/>abstract class SortStrategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;abstract public void Sort( ArrayList list );<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategy\"<br \/>class QuickSort : SortStrategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public override void Sort(ArrayList list )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;list.Sort(); \/\/ Default is Quicksort<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"QuickSorted list \");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategy\"<br \/>class ShellSort : SortStrategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public override void Sort(ArrayList list )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/list.ShellSort();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"ShellSorted list \");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"ConcreteStrategy\"<br \/>class MergeSort : SortStrategy<br \/>{<br \/>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public override void Sort( ArrayList list )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/list.MergeSort();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(\"MergeSorted list \");<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/\/ \"Context\"<br \/>class SortedList<br \/>{<br \/>&nbsp;&nbsp;\/\/ Fields<br \/>&nbsp;&nbsp;private ArrayList list = new ArrayList();<br \/>&nbsp;&nbsp;private SortStrategy sortstrategy;<\/p>\n<p>&nbsp;&nbsp;\/\/ Constructors<br \/>&nbsp;&nbsp;public void SetSortStrategy( SortStrategy sortstrategy )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;this.sortstrategy = sortstrategy;<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;\/\/ Methods<br \/>&nbsp;&nbsp;public void Sort()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;sortstrategy.Sort( list );<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public void Add( string name )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;list.Add( name );<br \/>&nbsp;&nbsp;}<\/p>\n<p>&nbsp;&nbsp;public void Display()<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;foreach( string name in list )<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine( \" \" + name );<br \/>&nbsp;&nbsp;}<br \/>}<\/p>\n<p>\/**\/\/\/\/ &lt;summary&gt;<br \/>\/\/\/ StrategyApp test<br \/>\/\/\/ &lt;\/summary&gt;<br \/>public class StrategyApp<br \/>{<br \/>&nbsp;&nbsp;public static void Main( string[] args )<br \/>&nbsp;&nbsp;{<br \/>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Two contexts following different strategies<br \/>&nbsp;&nbsp;&nbsp;&nbsp;SortedList studentRecords = new SortedList( );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Add( \"Samual\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Add( \"Jimmy\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Add( \"Sandra\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Add( \"Anna\" );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Add( \"Vivek\" );<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.SetSortStrategy( new QuickSort() );<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Sort();<br \/>&nbsp;&nbsp;&nbsp;&nbsp;studentRecords.Display();<br \/>&nbsp;&nbsp;}<br \/>}<br \/><\/code><\/p>\n<p>\u516d\u3001 \u5728\u4ec0\u4e48\u60c5\u51b5\u4e0b\u5e94\u5f53\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f<br \/>\u5728\u4e0b\u9762\u7684\u60c5\u51b5\u4e0b\u5e94\u5f53\u8003\u8651\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff1a<\/p>\n<p>1. \u5982\u679c\u5728\u4e00\u4e2a\u7cfb\u7edf\u91cc\u9762\u6709\u8bb8\u591a\u7c7b\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u4ec5\u5728\u4e8e\u5b83\u4eec\u7684\u884c\u4e3a\uff0c\u90a3\u4e48\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u53ef\u4ee5\u52a8\u6001\u5730\u8ba9\u4e00\u4e2a\u5bf9\u8c61\u5728\u8bb8\u591a\u884c\u4e3a\u4e2d\u9009\u62e9\u4e00\u79cd\u884c\u4e3a\u3002<\/p>\n<p>2. \u4e00\u4e2a\u7cfb\u7edf\u9700\u8981\u52a8\u6001\u5730\u5728\u51e0\u79cd\u7b97\u6cd5\u4e2d\u9009\u62e9\u4e00\u79cd\u3002\u90a3\u4e48\u8fd9\u4e9b\u7b97\u6cd5\u53ef\u4ee5\u5305\u88c5\u5230\u4e00\u4e2a\u4e2a\u7684\u5177\u4f53\u7b97\u6cd5\u7c7b\u91cc\u9762\uff0c\u800c\u8fd9\u4e9b\u5177\u4f53\u7b97\u6cd5\u7c7b\u90fd\u662f\u4e00\u4e2a\u62bd\u8c61\u7b97\u6cd5\u7c7b\u7684\u5b50\u7c7b\u3002\u6362\u8a00\u4e4b\uff0c\u8fd9\u4e9b\u5177\u4f53\u7b97\u6cd5\u7c7b\u5747\u6709\u7edf\u4e00\u7684\u63a5\u53e3\uff0c\u7531\u4e8e\u591a\u6001\u6027\u539f\u5219\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u4efb\u4f55\u4e00\u4e2a\u5177\u4f53\u7b97\u6cd5\u7c7b\uff0c\u5e76\u53ea\u6301\u6709\u4e00\u4e2a\u6570\u636e\u7c7b\u578b\u662f\u62bd\u8c61\u7b97\u6cd5\u7c7b\u7684\u5bf9\u8c61\u3002<\/p>\n<p>3. \u4e00\u4e2a\u7cfb\u7edf\u7684\u7b97\u6cd5\u4f7f\u7528\u7684\u6570\u636e\u4e0d\u53ef\u4ee5\u8ba9\u5ba2\u6237\u7aef\u77e5\u9053\u3002\u7b56\u7565\u6a21\u5f0f\u53ef\u4ee5\u907f\u514d\u8ba9\u5ba2\u6237\u7aef\u6d89\u53ca\u5230\u4e0d\u5fc5\u8981\u63a5\u89e6\u5230\u7684\u590d\u6742\u7684\u548c\u53ea\u4e0e\u7b97\u6cd5\u6709\u5173\u7684\u6570\u636e\u3002<\/p>\n<p>4. \u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u6709\u5f88\u591a\u7684\u884c\u4e3a\uff0c\u5982\u679c\u4e0d\u7528\u6070\u5f53\u7684\u6a21\u5f0f\uff0c\u8fd9\u4e9b\u884c\u4e3a\u5c31\u53ea\u597d\u4f7f\u7528\u591a\u91cd\u7684\u6761\u4ef6\u9009\u62e9\u8bed\u53e5\u6765\u5b9e\u73b0\u3002\u6b64\u65f6\uff0c\u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u628a\u8fd9\u4e9b\u884c\u4e3a\u8f6c\u79fb\u5230\u76f8\u5e94\u7684\u5177\u4f53\u7b56\u7565\u7c7b\u91cc\u9762\uff0c\u5c31\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u96be\u4ee5\u7ef4\u62a4\u7684\u591a\u91cd\u6761\u4ef6\u9009\u62e9\u8bed\u53e5\uff0c\u5e76\u4f53\u73b0\u9762\u5411\u5bf9\u8c61\u8bbe\u8ba1\u7684\u6982\u5ff5\u3002<\/p>\n<p>\u4e03\u3001 \u7b56\u7565\u6a21\u5f0f\u7684\u4f18\u70b9\u548c\u7f3a\u70b9<br \/>\u7b56\u7565\u6a21\u5f0f\u6709\u5f88\u591a\u4f18\u70b9\u548c\u7f3a\u70b9\u3002\u5b83\u7684\u4f18\u70b9\u6709\uff1a<\/p>\n<p>1. \u7b56\u7565\u6a21\u5f0f\u63d0\u4f9b\u4e86\u7ba1\u7406\u76f8\u5173\u7684\u7b97\u6cd5\u65cf\u7684\u529e\u6cd5\u3002\u7b56\u7565\u7c7b\u7684\u7b49\u7ea7\u7ed3\u6784\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b97\u6cd5\u6216\u884c\u4e3a\u65cf\u3002\u6070\u5f53\u4f7f\u7528\u7ee7\u627f\u53ef\u4ee5\u628a\u516c\u5171\u7684\u4ee3\u7801\u79fb\u5230\u7236\u7c7b\u91cc\u9762\uff0c\u4ece\u800c\u907f\u514d\u91cd\u590d\u7684\u4ee3\u7801\u3002<\/p>\n<p>2. \u7b56\u7565\u6a21\u5f0f\u63d0\u4f9b\u4e86\u53ef\u4ee5\u66ff\u6362\u7ee7\u627f\u5173\u7cfb\u7684\u529e\u6cd5\u3002\u7ee7\u627f\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u5982\u679c\u4e0d\u662f\u7528\u7b56\u7565\u6a21\u5f0f\uff0c\u90a3\u4e48\u4f7f\u7528\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u73af\u5883\u7c7b\u5c31\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u5b50\u7c7b\uff0c\u6bcf\u4e00\u4e2a\u5b50\u7c7b\u63d0\u4f9b\u4e00\u4e2a\u4e0d\u540c\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u3002\u4f46\u662f\uff0c\u8fd9\u6837\u4e00\u6765\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u4f7f\u7528\u8005\u5c31\u548c\u7b97\u6cd5\u6216\u884c\u4e3a\u672c\u8eab\u6df7\u5728\u4e00\u8d77\u3002\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e00\u79cd\u7b97\u6cd5\u6216\u91c7\u53d6\u54ea\u4e00\u79cd\u884c\u4e3a\u7684\u903b\u8f91\u5c31\u548c\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u903b\u8f91\u6df7\u5408\u5728\u4e00\u8d77\uff0c\u4ece\u800c\u4e0d\u53ef\u80fd\u518d\u72ec\u7acb\u6f14\u5316\u3002\u7ee7\u627f\u4f7f\u5f97\u52a8\u6001\u6539\u53d8\u7b97\u6cd5\u6216\u884c\u4e3a\u53d8\u5f97\u4e0d\u53ef\u80fd\u3002<\/p>\n<p>3. \u4f7f\u7528\u7b56\u7565\u6a21\u5f0f\u53ef\u4ee5\u907f\u514d\u4f7f\u7528\u591a\u91cd\u6761\u4ef6\u8f6c\u79fb\u8bed\u53e5\u3002\u591a\u91cd\u8f6c\u79fb\u8bed\u53e5\u4e0d\u6613\u7ef4\u62a4\uff0c\u5b83\u628a\u91c7\u53d6\u54ea\u4e00\u79cd\u7b97\u6cd5\u6216\u91c7\u53d6\u54ea\u4e00\u79cd\u884c\u4e3a\u7684\u903b\u8f91\u4e0e\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u903b\u8f91\u6df7\u5408\u5728\u4e00\u8d77\uff0c\u7edf\u7edf\u5217\u5728\u4e00\u4e2a\u591a\u91cd\u8f6c\u79fb\u8bed\u53e5\u91cc\u9762\uff0c\u6bd4\u4f7f\u7528\u7ee7\u627f\u7684\u529e\u6cd5\u8fd8\u8981\u539f\u59cb\u548c\u843d\u540e\u3002<\/p>\n<p>\u7b56\u7565\u6a21\u5f0f\u7684\u7f3a\u70b9\u6709\uff1a<\/p>\n<p>1. \u5ba2\u6237\u7aef\u5fc5\u987b\u77e5\u9053\u6240\u6709\u7684\u7b56\u7565\u7c7b\uff0c\u5e76\u81ea\u884c\u51b3\u5b9a\u4f7f\u7528\u54ea\u4e00\u4e2a\u7b56\u7565\u7c7b\u3002\u8fd9\u5c31\u610f\u5473\u7740\u5ba2\u6237\u7aef\u5fc5\u987b\u7406\u89e3\u8fd9\u4e9b\u7b97\u6cd5\u7684\u533a\u522b\uff0c\u4ee5\u4fbf\u9002\u65f6\u9009\u62e9\u6070\u5f53\u7684\u7b97\u6cd5\u7c7b\u3002\u6362\u8a00\u4e4b\uff0c\u7b56\u7565\u6a21\u5f0f\u53ea\u9002\u7528\u4e8e\u5ba2\u6237\u7aef\u77e5\u9053\u6240\u6709\u7684\u7b97\u6cd5\u6216\u884c\u4e3a\u7684\u60c5\u51b5\u3002<\/p>\n<p>2. \u7b56\u7565\u6a21\u5f0f\u9020\u6210\u5f88\u591a\u7684\u7b56\u7565\u7c7b\u3002\u6709\u65f6\u5019\u53ef\u4ee5\u901a\u8fc7\u628a\u4f9d\u8d56\u4e8e\u73af\u5883\u7684\u72b6\u6001\u4fdd\u5b58\u5230\u5ba2\u6237\u7aef\u91cc\u9762\uff0c\u800c\u5c06\u7b56\u7565\u7c7b\u8bbe\u8ba1\u6210\u53ef\u5171\u4eab\u7684\uff0c\u8fd9\u6837\u7b56\u7565\u7c7b\u5b9e\u4f8b\u53ef\u4ee5\u88ab\u4e0d\u540c\u5ba2\u6237\u7aef\u4f7f\u7528\u3002\u6362\u8a00\u4e4b\uff0c\u53ef\u4ee5\u4f7f\u7528\u4eab\u5143\u6a21\u5f0f\u6765\u51cf\u5c11\u5bf9\u8c61\u7684\u6570\u91cf\u3002<\/p>\n<p>\u516b\u3001 \u5176\u5b83<br \/>\u7b56\u7565\u6a21\u5f0f\u4e0e\u5f88\u591a\u5176\u5b83\u7684\u6a21\u5f0f\u90fd\u6709\u7740\u5e7f\u6cdb\u7684\u8054\u7cfb\u3002Strategy\u5f88\u5bb9\u6613\u548cBridge\u6a21\u5f0f\u76f8\u6df7\u6dc6\u3002\u867d\u7136\u5b83\u4eec\u7ed3\u6784\u5f88\u76f8\u4f3c\uff0c\u4f46\u5b83\u4eec\u5374\u662f\u4e3a\u89e3\u51b3\u4e0d\u540c\u7684\u95ee\u9898\u800c\u8bbe\u8ba1\u7684\u3002Strategy\u6a21\u5f0f\u6ce8\u91cd\u4e8e\u7b97\u6cd5\u7684\u5c01\u88c5\uff0c\u800cBridge\u6a21\u5f0f\u6ce8\u91cd\u4e8e\u5206\u79bb\u62bd\u8c61\u548c\u5b9e\u73b0\uff0c\u4e3a\u4e00\u4e2a\u62bd\u8c61\u4f53\u7cfb\u63d0\u4f9b\u4e0d\u540c\u7684\u5b9e\u73b0\u3002Bridge\u6a21\u5f0f\u4e0eStrategy\u6a21\u5f0f\u90fd\u5f88\u597d\u7684\u4f53\u73b0\u4e86&#8221;Favor composite over inheritance&#8221;\u7684\u89c2\u70b9\u3002<\/p>\n<p>\u63a8\u8350\u5927\u5bb6\u8bfb\u4e00\u8bfb\u300aIoC \u5bb9\u5668\u548cDependency Injection \u6a21\u5f0f\u300b\uff0c\u4f5c\u8005Martin Fowler\u3002\u7f51\u4e0a\u53ef\u4ee5\u627e\u5230\u4e2d\u6587\u7248\u7684PDF\u6587\u4ef6\u3002\u4e3a\u7b56\u7565\u6a21\u5f0f\u7684\u5b9e\u65bd\u63d0\u4f9b\u4e86\u4e00\u4e2a\u975e\u5e38\u597d\u7684\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001 \u7b56\u7565\uff08Strategy\uff09\u6a21\u5f0f\u7b56\u7565\u6a21\u5f0f\u7684\u7528\u610f\u662f\u9488\u5bf9\u4e00\u7ec4\u7b97\u6cd5\uff0c\u5c06\u6bcf&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-112","post","type-post","status-publish","format-standard","hentry","category-Note"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}