HeadFirst 設計模式一及其c#實現

來源:互聯網
上載者:User

 設計原則一:找出應用中可能需要變化之處,把他們獨立出來,不要把他們和不需變化的代碼混在一起。這樣可以很容易的改動或者封裝需要變化的部分;

設計原則二:針對介面編程;而不是針對實現編程。

 

下邊是c#代碼的實現:

 

 public interface IFlyBehavior<br /> {<br /> void Fly();<br /> }</p><p> public interface IQuackBehavior<br /> {<br /> void Quack();<br /> }</p><p>class FlyNoWay : IFlyBehavior<br /> {<br /> public void Fly()<br /> {<br /> Console.WriteLine("FlyNoWay");<br /> }<br /> }</p><p> class FlyWithWings : IFlyBehavior<br /> {<br /> public void Fly()<br /> {<br /> Console.WriteLine("FlyWithWings");<br /> }<br /> }</p><p>class MuteQuack : IQuackBehavior<br /> {<br /> public void Quack()<br /> {<br /> Console.WriteLine("MuteQuack");<br /> }<br /> }</p><p>class Quacks : IQuackBehavior<br /> {<br /> public void Quack()<br /> {<br /> Console.WriteLine("Quacks");<br /> }<br /> }</p><p> class RockerPowerBehavior : IFlyBehavior<br /> {<br /> public void Fly()<br /> {<br /> Console.WriteLine("RockerPowerBehavior");<br /> }<br /> }</p><p>class Spueak : IQuackBehavior<br /> {<br /> public void Quack()<br /> {<br /> Console.WriteLine("Spueak");<br /> }<br /> }</p><p>public abstract class Duck<br /> {<br /> public IFlyBehavior flyBehavior;<br /> public IQuackBehavior quackBehavior;</p><p> public Duck() { }<br /> public void PerformFly()<br /> {<br /> flyBehavior.Fly();<br /> }<br /> public void PerformBehavior()<br /> {<br /> quackBehavior.Quack();<br /> }<br /> public abstract void Display();<br /> public void Swim()<br /> {<br /> Console.WriteLine("鴨子都會遊泳。");<br /> }<br /> public void SetFly(IFlyBehavior flyBehavior)<br /> {<br /> this.flyBehavior = flyBehavior;<br /> }<br /> public void SetQuack(IQuackBehavior quackBehavior)<br /> {<br /> this.quackBehavior = quackBehavior;<br /> }<br /> }</p><p>class MarllarDuck : Duck<br /> {<br /> public MarllarDuck()<br /> {<br /> flyBehavior = new FlyWithWings();<br /> quackBehavior = new Quacks();<br /> }<br /> public override void Display()<br /> {<br /> Console.WriteLine("I am a Real Marllar Duck");<br /> }<br /> }</p><p>class ModelDuck : Duck<br /> {<br /> public ModelDuck()<br /> {<br /> this.flyBehavior = new FlyNoWay();<br /> this.quackBehavior = new MuteQuack();<br /> }<br /> public override void Display()<br /> {<br /> Console.WriteLine("I am a Model Duck.");<br /> }<br /> }</p><p> class Program<br /> {<br /> static void Main(string[] args)<br /> {<br /> //MarllarDuck marllarDuck = new MarllarDuck();</p><p> //marllarDuck.Display();<br /> //marllarDuck.PerformBehavior();<br /> //marllarDuck.PerformFly();<br /> //marllarDuck.Swim();<br /> Duck duck1 = new ModelDuck();<br /> duck1.PerformFly();<br /> duck1.SetFly(new RockerPowerBehavior());<br /> duck1.PerformFly();</p><p> Console.Read();<br /> }<br /> }<br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.