text: import{o as n,c as s,d as a}from"./app.4b6115df.js";const p='{"title":"设计原则","description":"","frontmatter":{},"headers":[{"level":3,"title":"设计原则","slug":"设计原则"},{"level":3,"title":"设计原则","slug":"设计原则-1"},{"level":3,"title":"设计原则","slug":"设计原则-2"},{"level":3,"title":"策略模式","slug":"策略模式"},{"level":3,"title":"笔记","slug":"笔记"}],"relativePath":"design/chapter01.md","lastUpdated":1632190622475}',t={},o=[a('<h3 id="设计原则" tabindex="-1">设计原则 <a class="header-anchor" href="#设计原则" aria-hidden="true">#</a></h3><div class="language-"><pre><code>当涉及“维护”时，为了“复用”目的而使用继承，解决并不完美。\n</code></pre></div><div class="language-"><pre><code>不管当初软件设计得多好，一段时间之后，总是需要成长与改变，否则软件就会“死亡”。\n</code></pre></div><h3 id="设计原则-1" tabindex="-1">设计原则 <a class="header-anchor" href="#设计原则-1" aria-hidden="true">#</a></h3><div class="language-"><pre><code>找出应用中可能需要变化之处，把它们独立出来，不要和那些不需要变化的代码混在一起。\n</code></pre></div><h3 id="设计原则-2" tabindex="-1">设计原则 <a class="header-anchor" href="#设计原则-2" aria-hidden="true">#</a></h3><div class="language-js"><pre><code>针对接口编程，而不是针对实现编程\n针对接口编程真正的意思是针对超类型编程\n抽象超类型可以是抽象类或者接口\n</code></pre></div><h3 id="策略模式" tabindex="-1">策略模式 <a class="header-anchor" href="#策略模式" aria-hidden="true">#</a></h3><p>定义了算法族，分别封装起来，让它们之间可以相互替换，此模式让算法的变化独立于使用算法的客户。</p><p>将鸭子的飞和叫这种可变的行为封装起来。 首先将鸭子的飞定义成一个接口</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">FlyBehavior</span> <span class="token punctuation">{</span>\n    <span class="token keyword">public</span>  <span class="token keyword">void</span> <span class="token function">fly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>然后定义叫的接口</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">QuackBehavior</span> <span class="token punctuation">{</span>\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">quack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>定义鸭子的抽象父类, 将飞的接口和叫叫的接口当成鸭子的属性，同时增加两个set方法，以便能动态的设置飞和叫</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Duck</span> <span class="token punctuation">{</span>\n    <span class="token class-name">FlyBehavior</span> flyBehavior<span class="token punctuation">;</span>\n    <span class="token class-name">QuackBehavior</span> quackBehavior<span class="token punctuation">;</span>\n    <span class="token keyword">public</span> <span class="token class-name">Duck</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">void</span> <span class="token function">display</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n\n    <span class="token keyword">public</span> <span class="token keyword">void</span>  <span class="token function">performFly</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        flyBehavior<span class="token punctuation">.</span><span class="token function">fly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">public</span> <span class="token keyword">void</span>  <span class="token function">performQuack</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        quackBehavior<span class="token punctuation">.</span><span class="token function">quack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">public</span>  <span class="token keyword">void</span>  <span class="token function">swim</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;All ducks float, even decoys!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setFlyBehavior</span><span class="token punctuation">(</span><span class="token class-name">FlyBehavior</span> flyBehavior<span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token keyword">this</span><span class="token punctuation">.</span>flyBehavior <span class="token operator">=</span> flyBehavior<span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setQuackBehavior</span><span class="token punctuation">(</span><span class="token class-name">QuackBehavior</span> quackBehavior<span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token keyword">this</span><span class="token punctuation">.</span>quackBehavior <span class="token operator">=</span> quackBehavior<span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>实现两个飞的接口的类</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FlyWithWings</span> <span class="token keyword">implements</span> <span class="token class-name">FlyBehavior</span> <span class="token punctuation">{</span>\n    <span class="token annotation punctuation">@Override</span>\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fly</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;I&#39;m  flying!!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre></div><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">FlyNoWay</span> <span class="token keyword">implements</span>  <span class="token class-name">FlyBehavior</span> <span class="token punctuation">{</span>\n    <span class="token annotation punctuation">@Override</span>\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fly</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;I can&#39;t fly&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>实现一个叫的接口的类</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Quack</span> <span class="token keyword">implements</span> <span class="token class-name">QuackBehavior</span> <span class="token punctuation">{</span>\n    <span class="token annotation punctuation">@Override</span>\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">quack</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;Quack&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>实现一个具体的ModelDuck鸭子</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ModelDuck</span> <span class="token keyword">extends</span>  <span class="token class-name">Duck</span> <span class="token punctuation">{</span>\n\n    <span class="token keyword">public</span> <span class="token class-name">ModelDuck</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        flyBehavior <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FlyNoWay</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        quackBehavior <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Quack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n    <span class="token annotation punctuation">@Override</span>\n    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">display</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot; I&#39;m a model duck&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n</code></pre></div><p>测试程序</p><div class="language-java"><pre><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">HelloWorld</span> <span class="token punctuation">{</span>\n    <span class="token keyword">public</span>  <span class="token keyword">static</span> <span class="token keyword">void</span>  <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>\n        <span class="token class-name">Duck</span> model <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ModelDuck</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        model<span class="token punctuation">.</span><span class="token function">performFly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        model<span class="token punctuation">.</span><span class="token function">setFlyBehavior</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FlyRockPowered</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n        model<span class="token punctuation">.</span><span class="token function">performFly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>\n    <span class="token punctuation">}</span>\n<span class="token punctuation">}</span>\n\n<span class="token comment">// I can&#39;t fly</span>\n<span class="token comment">// I&#39;m flying with rocket!</span>\n</code></pre></div><h3 id="笔记" tabindex="-1">笔记 <a class="header-anchor" href="#笔记" aria-hidden="true">#</a></h3><ol><li>关键将可变的抽离成接口，然后在父类中将接口定义成属性，增加可以改变属性的set方法。</li><li>同时实现自己行为的接口的类。（🌰中的飞和叫）</li><li>定义具体的子类，在构造方法中传入具体的行为的接口的类。</li><li>因为父类定义了可以改变属性接口的set方法，因为能变换具体的行为类。</li></ol>',26)];t.render=function(a,p,t,c,e,l){return n(),s("div",null,o)};export{p as __pageData,t as default};
  