日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C#實現簡單工廠模式_C#教程

作者:奮斗的大橙子 ? 更新時間: 2022-09-01 編程語言

情景:有一個怪獸,HP是100,現在勇士有可以使用武器將其打敗,有三種武器,木劍每次打擊20血,鐵劍每次50血,金剛劍每次100血,如果想要使用簡單工廠方式,怎么設計?

一.啥是簡單工廠?

通過專門定義一個類來負責創建其他類的實例,被創建的實例通常都具有共同的父類。

結構大概如下圖:

畫出場景的類圖

解釋:

  • 1.Sword是一個基類,通過其中有一個字段保存怪物的血量,還有一個虛方法是打擊怪物的方法
  • 2.有三個具體的武器的類,分別對應木劍、鐵劍、金剛劍,實現了各種對怪物打擊的邏輯
  • 3.CreateSwordFactory類,是具體實例化武器的類,通過客戶端的調用,可以傳入想要創建的武器。
  • 4.Program就是客戶端

二.具體的代碼

1.Sword.cs類

namespace SimpleFactory
{
        public class Sword
        {
                protected int monsterLife = 100;
                public virtual void beat()
                {
                        
                }
        }
}

2.WoodSword.cs

namespace SimpleFactory
{
????????public class WoodSword : Sword
????????{
????????????????public override void beat()
????????????????{
????????????????????????while (monsterLife > 0)
????????????????????????{
????????????????????????????????base.monsterLife -= 20;
????????????????????????????????Console.WriteLine("The Monster is already alive!");
????????????????????????}
????????????????????????Console.WriteLine("Excellent!The Monster is dead!"); 
????????????????}
????????}
}

3.IronSword.cs

namespace SimpleFactory
{
        public class IronSword:Sword
        {
                public override void beat()
                {
                        while (monsterLife > 0)
                        {
                                base.monsterLife -= 50;
                                Console.WriteLine("The Monster is already alive!");
                        }
                        Console.WriteLine("Excellent!The Monster is dead!"); 
                }
        }
}

4.DiamondSword.cs

namespace SimpleFactory
{
      public class DiamondSword:Sword
        {
              public override void beat()
                {
                        while (monsterLife > 0)
                        {
                                base.monsterLife -= 100;
                                Console.WriteLine("The Monster is already alive!");
                        }
                        Console.WriteLine("Excellent!The Monster is dead!"); 
                }
        }
}

5.CreateSwordFactory.cs

namespace SimpleFactory
{
        public class CreateSwordFactory
        {
                public static Sword CreateSword(string sword)
                {
                        Sword s = null;
                        switch (sword)
                        { 
                                case "WoodSword":
                                          s = new WoodSword();
                                        break;
                                case "IronSword":
                                          s = new IronSword();
                                        break;
                                case "DiamondSword":
                                          s = new DiamondSword();
                                        break;
                                default:
                                        break;
                        }
                        return s;
                }
        }
}

6.Program.cs

namespace SimpleFactory
{
        class Program
        {
                static void Main(string[] args)
                {
                        Sword s = CreateSwordFactory.CreateSword("WoodSword");
                        s.beat();
                        Console.WriteLine("----------------------");
                        s=CreateSwordFactory.CreateSword("IronSword");
                        s.beat();
                        Console.WriteLine("----------------------");
                        s = CreateSwordFactory.CreateSword("DiamondSword");
                        s.beat();
                }
        }
}

三.運行效果和總結

效果:

總結:

簡單工廠模式的優缺點:

優點:如下圖所示,這時候我們添加一個其他的劍,那么我不需要去修改我紅色區域的東西,僅僅修改CreateSwordFactory.cs這個類就行,然后這個類根據客戶端給出的具體產生什么劍再去實例化就可以了。不需要了解具體每一個劍是怎么被創建的。

缺點:以為過多的依賴于工廠類,簡單工廠模式違背了“開放封閉原則”,就是違背了“系統對擴展開放,對修改關閉”的原則,因為當我新增加一個劍的時候必須修改工廠類,相應的工廠類就需要重新編譯一遍。

原文鏈接:https://www.cnblogs.com/dcz2015/p/5261609.html

欄目分類
最近更新