C#實現(xiàn)簡單工廠模式
情景:有一個怪獸,HP是100,現(xiàn)在勇士有可以使用武器將其打敗,有三種武器,木劍每次打擊20血,鐵劍每次50血,金剛劍每次100血,如果想要使用簡單工廠方式,怎么設計?
一.啥是簡單工廠?
通過專門定義一個類來負責創(chuàng)建其他類的實例,被創(chuàng)建的實例通常都具有共同的父類。
結(jié)構大概如下圖:
畫出場景的類圖
解釋:
- 1.Sword是一個基類,通過其中有一個字段保存怪物的血量,還有一個虛方法是打擊怪物的方法
- 2.有三個具體的武器的類,分別對應木劍、鐵劍、金剛劍,實現(xiàn)了各種對怪物打擊的邏輯
- 3.CreateSwordFactory類,是具體實例化武器的類,通過客戶端的調(diào)用,可以傳入想要創(chuàng)建的武器。
- 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(); } } }
三.運行效果和總結(jié)
效果:
總結(jié):
簡單工廠模式的優(yōu)缺點:
優(yōu)點:如下圖所示,這時候我們添加一個其他的劍,那么我不需要去修改我紅色區(qū)域的東西,僅僅修改CreateSwordFactory.cs這個類就行,然后這個類根據(jù)客戶端給出的具體產(chǎn)生什么劍再去實例化就可以了。不需要了解具體每一個劍是怎么被創(chuàng)建的。
缺點:以為過多的依賴于工廠類,簡單工廠模式違背了“開放封閉原則”,就是違背了“系統(tǒng)對擴展開放,對修改關閉”的原則,因為當我新增加一個劍的時候必須修改工廠類,相應的工廠類就需要重新編譯一遍。
到此這篇關于C#簡單工廠模式的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Unity使用物理引擎實現(xiàn)多旋翼無人機的模擬飛行
這篇文章主要介紹了Unity使用物理引擎實現(xiàn)多旋翼無人機的模擬飛行,包括了詳細的原理介紹和代碼實現(xiàn),對物理引擎感興趣的同學,可以參考下2021-04-04如何在Mac系統(tǒng)使用Visual Studio Code運行Python
這篇文章主要介紹了Mac使用Visual Studio Code運行Python環(huán)境的方法,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
這篇文章主要介紹了C#簡單訪問SQLite數(shù)據(jù)庫的方法,涉及SQLite數(shù)據(jù)庫的下載、安裝及使用C#連接、查詢SQLIte數(shù)據(jù)庫的相關技巧,需要的朋友可以參考下2016-07-07