Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
DDD TW 7th 導讀 - Ch11 Factory
Search
James Wang
July 03, 2019
Programming
520
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
DDD TW 7th 導讀 - Ch11 Factory
James Wang
July 03, 2019
Transcript
11 slides
Factory in DDD 2019/07/03 DDD TW James Wang
大家知道工廠模式嗎? (知道的話今天導讀就到這邊了)
工廠模式 1. 很多變形:Abstract Factory、Factory Method、Builder…… 2. 透過封裝內部結構,簡化 Client 端「建立」和「重建」複雜物件流程。 Client
FACTORY Product new create product
在 DDD 中,該如何運用工廠模式?
Factory in DDD 當建立一個物件或建立整個 Aggregate 時,如果建立的工作很複雜,或者暴露過多的 內部結構,則可以使用 Factory 進行封裝。 使用動機:
• 將建立複雜物件的實例和 Aggregate 的職責轉移給單獨的物件。 • 封裝建立 Aggregate 時所有職責,譬如創建物件時的檢核。舉例來說,建立「狗」 的物件,傳入屬性「狗腿」個數,則需檢核「狗腿」不能大於 4 條腿。 • 透過工廠,建立 Aggregate 時要把它作為一個整體,並確保它滿足固定規則。
Aggregate Root 中的 Factory • 最常見的運用之一。 • 主要職責是一口氣將該 Aggregate Root
底下所有 Aggregate 建立起來。 • 也負責建立 Aggregate 所需邏輯與檢核。
一般的寫法 建構式中做完初始化所有功能。 public class Order : EntityBase, IAggregateRoot { public
decimal Total { get; private set; } public Order(Guid id, IEnumerable<Product> orderItems) : base(id) { // 針對 Products 檢核(略) // 計算總金額 var total = 0m; foreach (var item in orderItems) { if (item.UnitPrice > 0) { total += item.UnitPrice; } else { throw new ArgumentOutOfRangeException(nameof(item.UnitPrice)); } } Total = total; } }
套用工廠寫法 透過工廠呼叫 Aggregate Root。 public class OrderFactory { public Order
Create(Guid id, IEnumerable<Product> orderItems) { // 針對 Products 檢核(略) // 計算總金額(略) return new Order(id, orderItems); } }
套用工廠寫法 另外一種寫法。 public class Order : EntityBase, IAggregateRoot { public
List<Product> OrderItems { get; } = new List<Product>(); private Order(Guid id) : base(id) { } public static Order NewOrder(Guid id) { return new Order(id); } public void Create(IEnumerable<Product> orderItems) { // 針對 Products 檢核(略) // 計算總金額(略) } }
各式各樣的工廠 • Domain Service Factory • Entity Factory • Value
Object Factory 簡單說,當你發現建造物件很複雜時,都能套用工廠模式。 BUT… 有時候其實是你設計不好導致太複雜。 所以套用工廠模式前,請回頭看看自己的設計是否有沒有問題。
Recap • 當覺得建立或重建物件很複雜或想要隱藏細節的地方,都能用工廠。 • 在 DDD 中,常用於 Aggregate Root,在建立 Aggregate
時要把它視為一個整體 ,並確保它滿足固定規則,保證 Aggregate 狀態的正確性。 • 使用工廠之前,先確認所謂的複雜是不是設計不良引起的。有些情況下只需要使 用建構子。 • 工廠是放置固定規則相關邏輯的合適地方。 • 透過工廠封裝,表達限界上下文的通用語言。
More Decks by James Wang
See All by James Wang
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
390
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.6k
AI 寫得更快,你真的懂嗎?
jame2408
0
350
實踐 BizDevOps 在遺留系統中的挑戰與策略
jame2408
0
600
DDD 中的橋樑:透過有效建模與設計從戰略走向戰術
jame2408
0
620
從領域知識到架構設計
jame2408
1
430
淺談領域驅動設計
jame2408
1
740
淺談自動化測試
jame2408
0
490
Domain Driven Design The First 15 Years 導讀分享
jame2408
0
510
Other Decks in Programming
See All in Programming
flutter_test のその先へ — Flutter アプリの LLM 機能に「評価」という品質保証を
kosukesaigusa
0
210
疎結合とは何か?ソフトウェア設計における結合バランスについて考える #phpcon_niigata
kajitack
2
420
Kawaiicon: Kubernetes, it's no longer cool, but let's secure it anyway!
barnbarn
0
140
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
1.1k
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.6k
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
17
9.5k
特急いなほ as a Workflow / 20261107jaws-festa-2026-akita-lt
kasacchiful
PRO
0
110
Kotlin Fest 2026: Kotlin コンパイラに Contribute してみよう! - Kotlin/Wasm で深い再帰を書けるようにした話
ternbusty
1
420
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
240
テストだけで見つかる? Rubyでつくるメソッド発掘器
izawa
0
160
エラーチャンネルのボールを拾い続けて鍛えるエンジニアの地力 / phpcon-niigata2026
meihei3
1
500
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
770
Featured
See All Featured
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
760
Web Performance Is a Journey [PerfNow 2026]
tammyeverts
2
150
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
490
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.9k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
1
310
Paper Plane (Part 1)
katiecoart
PRO
3
12k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
640
Become a Pro
speakerdeck
PRO
31
6.4k
Ruling the World: When Life Gets Gamed
codingconduct
0
450
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
StorybookのUI Testing Handbookを読んだ
zakiyama
31
7.1k
Leo the Paperboy
mayatellez
11
2.5k