面試官:談一下你對DDD的理解?我:馬什麼梅?

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領域模型(domain model)是對領域內的概念類或現實世界中對象的可視化表示。領域模型也稱爲概念模型、領域對象模型和分析對象模型。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"——《UML和模式應用》"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們在日常開發中,經常針對一些功能點爭論“這個功能不應該我改,應該是你那邊改”,最終被妥協改了之後都改不明白爲什麼這個功能要在自己這邊改。區別於傳統的架構設計,領域驅動設計(DDD)也許在這個時候能幫助你做到清晰的劃分。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"什麼是DDD"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"領域驅動設計最初由Eric Evans提出,但是多年以來一直停留在理念階段,真正能實現並且落地的項目和公司少之又少,而進來阿里內部其實在大力推行DDD的理念,它主要可以幫助我們解決傳統單體式集中架構難以快速響應業務需求落地的問題,並且針對中臺和微服務盛行的場景做出指導。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DDD爲我們提供的是架構設計的方法論,既面向技術也面向業務,從業務的角度來把握設計方案。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"DDD的作用"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f8/f8f37c88e3d9f01caacf80d2cc3fc520.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"統一思想"},{"type":"text","text":":統一項目各方業務、產品、開發對問題的認知,而不是開發和產品統一,業務又和產品統一從而產生分歧。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"明確分工"},{"type":"text","text":":域模型需要明確定義來解決方方面面的問題,而針對這些問題則形成了團隊分鐘的理解。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"反映變化"},{"type":"text","text":":需求是不斷變化的,因此我們的模型也是在不斷的變化的。領域模型則可以真實的反映這些變化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"邊界分離"},{"type":"text","text":":領域模型與數據模型分離,用領域模型來界定哪些需求在什麼地方實現,保持結構清晰。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"DDD的概念"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"實體"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有唯一標誌的核心領域對象,且這個標誌在整個軟件生命週期中都不會發生變化。這個概念和我們平時軟件模型中和數據庫打交道的Model實例比較接近,唯一不同的是DDD中這些實體會包含與該實體相關的業務邏輯,它是操作行爲的載體。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"值對象"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"依附於實體存在,通過對象屬性來識別的對象,它將一些相關的實體屬性打包在一起處理,形成一個新的對象。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"舉個栗子:比如用戶實體,包含用戶名、密碼、年齡、地址,地址又包含省市區等屬性,而將省市區這些屬性打包成一個屬性集合就是值對象。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"聚合"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"實體和值對象表現的是個體的能力,而我們的業務邏輯往往很複雜,依賴個體是無法完成的,這時候就需要多個實體和值對象一起協同工作,而這個協同的組織就是聚合。聚合是數據修改和持久化的基本單元,同一個聚合內要保證事務的一致性,所以在設計的時候要保證聚合的設計拆分到最小化以保證效率和性能。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"聚合根"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"也叫做根實體,一個特殊的實體,它是聚合的管理者,代表聚合的入口,抓住聚合根可以抓住整個聚合。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"領域服務"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有些領域的操作是一些動詞,並不能簡單的把他們歸類到某個實體或者值對象中。這樣的行爲從領域中識別出來之後應該將它聲明成一個服務,它的作用僅僅是爲領域提供相應的功能。"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"領域事件"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在特定的領域由用戶動作觸發,表示發生在過去的事件。比如充值成功、充值失敗的事件。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"四種模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/33/33ea451ef23123bf53420de9f6bce5c4.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"失血模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"模型中只有簡單的get set方法,是對一個實體最簡單的封裝,其他所有的業務行爲由服務類來完成。"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"@Data\n@ToString\npublic class User {\n private Long id;\n private String username;\n private String password;\n private Integer status;\n private Date createdAt;\n private Date updatedAt;\n private Integer isDeleted;\n}\n\npublic class UserService{\n public boolean isActive(User user){\n return user.getStatus().equals(StatusEnum.ACTIVE.getCode());\n }\n}"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"貧血模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在失血模型基礎之上聚合了業務領域行爲,領域對象的狀態變化停留在內存層面,不關心數據持久化。"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"@Data\n@ToString\npublic class User {\n private Long id;\n private String username;\n private String password;\n private Integer status;\n private Date createdAt;\n private Date updatedAt;\n private Integer isDeleted;\n \n public boolean isActive(User user){\n return user.getStatus().equals(StatusEnum.ACTIVE.getCode());\n }\n \n public void setUsername(String username){\n return username.trim();\n }\n}\n"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"充血模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在貧血模型基礎上,負責數據的持久化。"}]},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"@Data\n@ToString\npublic class User {\n private Long id;\n private String username;\n private String password;\n private Integer status;\n private Date createdAt;\n private Date updatedAt;\n private Integer isDeleted;\n \n private UserRepository userRepository;\n \n public boolean isActive(User user){\n return user.getStatus().equals(StatusEnum.ACTIVE.getCode());\n }\n \n public void setUsername(String username){\n this.username = username.trim();\n userRepository.update(user);\n }\n}"}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"脹血模型"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"service都不需要,所有的業務邏輯、數據存儲都放到一個類中。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"對於DDD來說,失血和脹血都是不合適的,失血太輕量沒有聚合,脹血那是初學者才這樣寫代碼。那麼充血模型和貧血模型該怎麼選擇?充血模型依賴repository接口,與數據存儲緊密相關,有破壞程序穩定性的風險。"}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"建模方法"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d2/d262ffd720be14b96cfb85b81f9601b7.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"用例分析法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"用例分析法是領域建模最簡單可行的方式。大致可以分爲獲取用例、收集實體、添加關聯、添加屬性、模型精化幾個步驟。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":"1","normalizeStart":1},"content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"獲取用例:提取領域規則描述"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"收集實體:定位實體,"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"添加關聯:兩個實體間用動詞關聯起來"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"添加屬性:獲取實體屬性"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"text","text":"模型精化:可選的步驟,可以用UML的泛華和組合來表達模型間的關係,同時可以做子領域的劃分"}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"四色建模法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"四色建模法源於《Java Modeling In Color With UML》,它是一種模型的分析和設計方法,通過把所有模型分爲四種類型,幫助模型做到清晰、可追溯。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"簡單來說,四色關注的是某個人的角色在某個地點的角色用某個東西的角色做了某件事情。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/60/60eafbffd13641a5c1f5e58edb07a52a.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"事件風暴法"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"事件風暴法類似頭腦風暴,簡單來說就是誰在何時基於什麼做了什麼,產生了什麼,影響了什麼事情。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ab/ab7d3d79cc68ec500fe10041e334188e.jpeg","alt":null,"title":"","style":[{"key":"width","value":"50%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"架構分層"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"區別於左圖傳統架構的分層,一般DDD分層會有一些變化。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Application:包含事件註冊、業務邏輯等"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Domain:聚合、實體、值對象"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"InfraStructure:基礎設施封裝、數據庫訪問等"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e7/e79df66bfc381bf2622c5df8cd5ff73b.jpeg","alt":"image.png","title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"總結"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"DDD是一套完善的方法論,他能幫助我們合理的對系統進行架構設計,同時,好的模板應該是在不斷的適應變化,而DDD也能幫助我們更快速更方便的支撐業務的發展。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e3/e3e2b1d9d8cc8edce05f39dc757198f3.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章