網站首頁 編程語言 正文
情景:有一個怪獸,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
相關推薦
- 2022-06-18 Qt?事件過濾器的具體實現_C 語言
- 2022-08-13 瀏覽器的任務隊列-宏任務、微任務的執行順序
- 2023-04-08 Swift?HTTP加載請求Loading?Requests教程_Swift
- 2022-12-28 C++數據結構之哈希算法詳解_C 語言
- 2022-10-16 Flask快速實現分頁效果示例_python
- 2023-06-02 Python利用CNN實現對時序數據進行分類_python
- 2023-01-14 ubuntu開機后ROS程序自啟動問題_Linux
- 2023-01-12 關于scipy.optimize函數使用及說明_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支