網站首頁 編程語言 正文
設計模式分類:
- 創建型模式。
- 結構型模式。
- 行為模式。
23種設計模式,如何記。面向對象的系統中有很多對象,創建型模式解決的問題就是如何創建對象,何時創建對象,它努力的讓代碼不要太多的關注對象的具體類型,不用關注對象的創建細節,而知需要了解對象的抽象類型,創建對象的工作由創建對象的工廠來實現。
面向對象的系統中,對象如何組織,采用什么樣的結構組織比較合理,這個是由結構型模式來處理的。合理的使用結構型模式可以使系統具備更好的靈活性、擴展性和維護性。
行為模式規定了各個對象間的應該具備的職責。
嚴格來說,簡單工廠模式并不是23種標準模式之一,它是工廠家族中最簡單的模式,使用這個模式可以把對象的創建和對象的使用分離開,工廠只負責對象的創建,客戶端程序調用和使用對象,客戶端程序無需創建對象。這樣對象的創建放在一起,方便維護和擴展。現實中生產鞋子的工廠負責生產鞋子,我們不需要知道生產的鞋子的具體類型。
如圖所示:右上角是一個產品接口,我們可以使用接口或抽象類來定義一個產品對象。Animal類中有一個行為吃,Animal類派生出兩個子類:Dog、Penguin。這兩個類分別實現了吃的動作,這兩個動物其實是簡單工廠中具體的產品,通過他們實現抽象的產品;這些動物該如何去創建呢,我們可以用動物工廠AnimalFactory來創建具體的動物,AnimalFactory類中有一個GetAnimal的方法,在這個方法里我們可以根據傳進去的參數來創建具體的產品,工廠類和產品類是依賴的關系。
在客戶端,它關聯了抽象的產品類Animal和工廠類AnimalFactory,對客戶來說,他不需要了解具體的產品,只需要告訴工廠,需要什么具體的動物,動物工廠就會根據客戶端的要求來創建某個動物,通過簡單工廠模式,使客戶端程序與具體的產品之間減少耦合度。
示例:
抽象動物類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { /* 動物抽象類 * 抽象產品 */ public abstract class Animal { public abstract void Eat(); } }
具體動物狗類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { /* 具體的產品類,實現抽象產品類 */ public class Dog:Animal { // 實現抽象方法 public override void Eat() { Console.WriteLine("狗在吃飯!"); } } }
具體動物企鵝類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { /* 具體產品類,實現抽象產品類 */ public class Penguin :Animal { // 實現抽象方法 public override void Eat() { Console.WriteLine("企鵝在吃飯!"); } } }
動物工廠類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { /* 動物工廠類:實現具體的動物 */ public class AnimalFactory { ////// 根據客戶的選擇創建動物對象 /// /// 動物編號 ///public Animal GetAnimal(int witch) { Animal am = null; switch (witch) { case 1: am = new Dog(); break; case 2: am = new Penguin(); break; } return am; } } }
主程序調用測試
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { class Program { static void Main(string[] args) { // 得到具體的動物 (里氏替換原則) Animal am = new AnimalFactory().GetAnimal(1); // 調用Eat()方法 am.Eat(); // 輸出狗在吃飯 Console.ReadKey(); } } }
測試結果:
使用接口實現簡單工廠模式
需求:使用面向對象的方式設計一個系統,描述使用卡車從事貨運,使用公共汽車從事客運。使用工廠模式實現。
1、汽車接口:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { ////// 汽車接口 /// public interface ICar { ////// 描述汽車從事的活動 /// void Work(); } }
2、分別定義卡車類(Truck)和公共汽車類(Bus)實現ICar接口
Truck類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { ////// 卡車類 /// public class Truck : ICar { ////// 卡車從事的活動 /// public void Work() { Console.WriteLine("卡車從事貨運"); } } }
Bus類:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { ////// 公共汽車類 /// public class Bus:ICar { ////// 公共汽車從事的活動 /// public void Work() { Console.WriteLine("公共汽車從事客運"); } } }
3、定義汽車的工廠類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { ////// 汽車工廠類:返回具體的汽車類 /// public class CarFactory { ////// 根據汽車編號創建具體的汽車對象 /// /// 汽車編號 ///public ICar GetCar(int witch) { ICar car = null; switch (witch) { case 1: car= new Truck(); break; case 2: car = new Bus(); break; } return car; } } }
4、主程序調用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 簡單工廠模式 { class Program { static void Main(string[] args) { // 得到具體的汽車 ICar car = new CarFactory().GetCar(2); // 調用Work()方法 car.Work(); Console.ReadKey(); } } }
5、程序運行結果
簡單工廠模式的缺點:
增加具體產品時,需要修改工廠類里面生成具體產品的方法,這就違反了開閉原則。具體產品經常發生變化時,不建議使用簡單工廠模式。
代碼下載地址:點擊下載
原文鏈接:https://www.cnblogs.com/dotnet261010/p/7352045.html
相關推薦
- 2023-03-11 Go語言break跳轉語句怎么使用_Golang
- 2022-03-11 解決 fatal error LNK1120: 1 unresolved externals 問題
- 2022-05-03 SQL?Server查詢某個字段在哪些表中存在_MsSql
- 2022-09-22 nodeSelector:Pod 定向調度
- 2022-12-12 flutter?Bloc?add兩次只響應一次問題解析_Android
- 2022-02-27 解決 idea突然使用debug功能時項目啟動一半卡住沒反應也不報錯
- 2022-08-27 python基礎篇之pandas常用基本函數匯總_python
- 2022-06-15 oracle多表簡單查詢實例代碼_oracle
- 最近更新
-
- 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同步修改后的遠程分支