架構必備的領域邊界劃分方法:職責驅動設計

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"寫該文章的目的是對過去一段時間自我學習知識的總結,通過對職責驅動設計(RDD)的學習發現通過該模式進行對領域職責進行劃分能夠得出很清晰的職責,能有效減少無結論的掰扯,後面部分也有些在實際中的運用,期望能和大家交流起來~"}]}]},{"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":1},"content":[{"type":"text","text":"1.爲什麼使用RDD?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在RDD中,我們認爲“軟件對象具有職責”,這個定義很符合人在社會羣體中分工協作的方式,軟件也是人編寫的,所以根據職責思考設計的軟件系統符合人的行爲習慣,同時更易於理解和管理。在微服務架構中不同系統由不同的組織和人負責,把系統當作對象(人),系統提供的接口就是對象(人)的職責。"}]},{"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":"underline"}],"text":"職責驅動設計的核心是考慮怎樣給對象分配職責,其適用於大到系統、小到對象等任何規模的軟件。職責分配的本質是分工,勞動分工是勞動生產率提高的主要原因"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}},{"type":"strong"}],"text":":"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}},{"type":"strong"}],"text":" 1. 熟練度的提高,專注於某個領域(降低複雜度)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}},{"type":"strong"}],"text":" 2. 時間的節約,同一個人在不同工作來回切換需要耗費大量時間"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}},{"type":"strong"}],"text":" 3. 人工發明的機器和應用(特定領域的工具)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2.如何給對象(元素)分配職責?"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic"},{"type":"color","attrs":{"color":"#FFA940","name":"user"}},{"type":"strong"}],"text":"分配職責應當從清晰的描述職責開始"},{"type":"text","text":",對於軟件領域對象來說,"},{"type":"text","marks":[{"type":"underline"}],"text":"領域模型"},{"type":"text","text":"描述了領域對象的屬性和關聯,對應類的屬性和引用,"},{"type":"text","marks":[{"type":"underline"}],"text":"用例模型"},{"type":"text","text":"包含一系列的行爲活動,對應類的方法。領域模型創建方式參考:"}]},{"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":"學習《UML和模式應用》、UDD、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","marks":[{"type":"strong"}],"text":"使用GRASP模式分配職責"},{"type":"text","text":",GRASP是通用職責分配模式(General Responsibility Assignment Software Patterns),是對一些基本的職責分配原則進行了命名和描述,共9種模式。"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#40A9FF","name":"blue"}},{"type":"strong"}],"text":"一些GRASP原則是對其他原則和設計模式的歸納,設計模式有上百種,只是記住GoF 23種設計模式就已經很困難了更別提還要記住每種模式的細節,因此需要對設計模式進行有效的歸類,GRASP中的原則描述了模式的本質,除了有助加速設計模式學習之外,對發現現有設計存在的問題也更有效,這就是歸納的價值"}]}]},{"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":"text","marks":[{"type":"strong"}],"text":"低耦合、高內聚"},{"type":"text","text":"時,我們具體是在談什麼?問題不在於耦合度高、內聚性低,而是在於其產生的負面影響,負面影響往往是在發生"},{"type":"text","marks":[{"type":"strong"}],"text":"變化"},{"type":"text","text":"時體現出來的,這些負面影響會影響到我們開發的效率、穩定性、可維護性、可擴展性、可複用性等等,整個GRASP的核心是如何"},{"type":"text","marks":[{"type":"color","attrs":{"color":"#050505","name":"user"}},{"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}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c8/c84098bf0f4de993cb12adf3ba900a45.png","alt":null,"title":"在學習過程中發現GRASP缺少結構化的展示歸納結果,通過我自己的理解把開發中常用的GoF設計模式、面向對象設計原則、架構設計原則和GRASP進行關聯。注:這個圖可能總結的還不夠準確,正在逐步學習修改。","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"3.GRASP職責分配模式"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.1 防止變異"}]},{"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":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":識別預計變化或不穩定之處,分配職責用以在這些變化之外創建穩定接口。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關原則和模式"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:間接性、多態"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GoF:大量模式"}]}]},{"type":"listitem","content":[{"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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.2 低耦合、高內聚"}]},{"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":"underline"}],"text":"耦合是對某元素與其他元素之間的連接、感知和依賴程度的度量,內聚是對元素職責的相關性和集中度的度量("},{"type":"text","text":"這裏的元素指類、系統、子系統等等),耦合和內聚是從不同角度看待問題,他們互相依賴的互相影響的:(以下兩點也可以反過來說)"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內聚過低,相關功能分散在不同模塊中,需要增加額外的耦合使這些功能聚合在一起,發生變更時影響多個模塊。"}]}]},{"type":"listitem","content":[{"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/2e/2e17eed54adf4a89b608e84e7d688c32.png","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.2.1 低耦合"}]},{"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":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":分配職責,使耦合儘可能低。利用這一原則評估可選方案。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關模式或原則"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:防止變異"}]}]}]},{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"緊密耦合的系統在開發階段有以下的缺點"},{"type":"text","text":":"}]},{"type":"numberedlist","attrs":{"start":null,"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":"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":"size","attrs":{"size":14}},{"type":"strong"}],"text":"解讀:"},{"type":"text","text":"耦合表示元素之間存在依賴,當談論“耦合高”時,我們具體是在談論什麼呢?"},{"type":"text","marks":[{"type":"strong"}],"text":"是依賴產生的負面影響,所以低耦合的核心是解決不良依賴。"},{"type":"text","text":"高低是度量並不是評判耦合結果好壞的標準,使用“不良耦合”、“松耦合”描述的更爲準確。不良耦合產生的負面影響主要有兩點:"}]},{"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","marks":[{"type":"underline"}],"text":"依賴關係本身"},{"type":"text","text":"錯綜複雜難以維護和理解,很容易產生遺漏和問題(這點針對人,人處理複雜性事物時能力是侷限的)。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"與"},{"type":"text","marks":[{"type":"underline"}],"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","text":"那麼如何做呢?先對依賴關係的好壞進行評估"},{"type":"text","marks":[{"type":"strong"}],"text":": 依賴方式、依賴方向、依賴鏈路"},{"type":"text","text":"。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/386ccf92d588af7c9ef6fa24f266b98c.png","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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/57/573b71189358267eb4b5df5164309909.jpeg","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"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","text":"解決不良依賴:"}]},{"type":"numberedlist","attrs":{"start":null,"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":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"依賴方向"},{"type":"text","text":":使用單向依賴,去除或弱化雙向依賴,不使用循環依賴。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"依賴鏈路"},{"type":"text","text":":遵守最少認知原則。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":3,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"依賴方式"},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.2.2 高內聚"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"內聚是對元素職責的相關性和集中度的度量。"}]},{"type":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案:"},{"type":"text","text":"按照相關性分配職責,可保持較高的內聚。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"優點"},{"type":"text","text":":"}]},{"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":"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"低內聚的缺點"},{"type":"text","text":":內聚性較低的類要做許多不相關的工作,或需要完成大量的工作,這樣的類會導致以下問題:"}]},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2c/2c99b49a8ab578282a3c2db1ac6ef8aa.png","alt":null,"title":"image.png","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}},{"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":"例子:A的變更影響從3個模塊變爲1個。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f1/f18ef00bcf47fe02353b1478a077c428.png","alt":null,"title":"image.png","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}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"3.2.3 小結"}]},{"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/4b/4b23f7ca771fbe7854ed70f55bcd921d.png","alt":"image.png","title":"image.png","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.3 創建者"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"創建者指導我們分配那些與創建對象有關的職責。如此選擇是爲了保持低耦合。"}]},{"type":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":滿足以下條件之一時,將創建類A的職責分配給類B("},{"type":"text","marks":[{"type":"color","attrs":{"color":"#FF7021","name":"orange"}}],"text":"當滿足1條以上時,通常首選包含或聚合"},{"type":"text","text":")"}]},{"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":"B“包含”或聚合A。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"B記錄A。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"B頻繁使用A。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"B具有A的初始化數據,該數據將在創建時傳遞給A。"}]}]}]},{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關模式或原則"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:低耦合"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GoF:具體工廠、抽象工廠"}]}]},{"type":"listitem","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注"},{"type":"text","text":":包含(作者在這裏標註了“”,因爲包含在uml是表達用例關係的,用來說明對象關係也可以)、聚合、整體-部分 看UML定義;包含強調了強依賴(A是B的子集,A屬於B,缺少了A時B不是整體),聚合是弱依賴(B由A組成,A不屬於B)"}]}]},{"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":"image","attrs":{"src":"https://static001.geekbang.org/infoq/d5/d5dfa330163a55d8343aa11c9644bc1b.png","alt":null,"title":"image.png","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}},{"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":"Order包含Goods(Order脫離Goods就失去了完整性,沒有存在的意義)"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"Order記錄相關的Goods。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"Goods初始化數據"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"情況一:只需要訂單上的Goods數據,這種情況Order具有Goods的初始化數據。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":1,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"情況二:訂單上的Goods數據不完整,這種情況Order只有Goods初始化數據的一小部分,Order不能做爲創建者。"}]}]}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.4 信息專家(or 專家)"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"“信息”不單指數據。"}]},{"type":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":把職責分配給信息專家,它具有實現這個職責所必需的信息"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"優點"},{"type":"text","text":":"}]},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關模式或原則"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:低耦合、高內聚"}]}]}]},{"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":":獲取所有買的商品總金額,Order和Goods是一對多的關係。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5c/5c6e39b3863bce98e3cc4c4d07534337.png","alt":"image.png","title":"image.png","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"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":"分析:Order本身關聯了Goods,並且理解Goods的結構。在圖例中Client通過Order獲取了Goods並做了邏輯運算得出商品總金額,這種做法產生了不必要的依賴增加了耦合數量,商品總金額計算的職責由Order承擔最合適。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/8e/8ef9930bfff8905b6c1d3b0c56e1d373.png","alt":"image.png","title":"image.png","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"延伸"},{"type":"text","text":":在某些情況下,該方案並不合適,通常是由於耦合與內聚問題產生的,如:誰應該把對象A存入數據庫?按照原則每個類都應該具有把自己持久化的能力。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.5 純虛構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"爲了保持良好的耦合和內聚,捏造業務上不存在的對象來承擔職責。"}]},{"type":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":對人爲製造的類分配一組高內聚的職責,該類並不代表問題領域的概念--虛構的事物,用以支持高內聚、低耦合和複用。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"優點"},{"type":"text","text":":"}]},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關原則和模式"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:低耦合、高內聚。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通常接納本來是基於專家模式所分配給領域類的職責。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"所有GoF設計模式都是純虛構,事實上所有其他設計模式也都是純虛構。"}]}]}]}]},{"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":":計算商品總數量。 根據專家模式計算商品總數量的職責也應該是分配給Order,照這樣分配下去商品相關的還會有:總重量、總體積、總XX,這時Order的職責就會越來越多也可能會產生額外的耦合,通過純虛構對象把這些職責分配出去能夠得到更好的設計。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/47/47d3c22458a8f819a21a1b025b8d5522.png","alt":"image.png","title":"image.png","style":null,"href":null,"fromPaste":true,"pastePass":true}},{"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":"通過虛構對象GoodsItems承擔和商品聚合計算相關的職責。"}]},{"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":":經常發現代碼中會使用Util、Handler、Service這樣的虛構類,缺點是這些類通常是單例並共用的,這些虛構類的職責會越來越多(一個Util類2000行代碼),創建和業務更相近的虛構對象才能便於理解和管理耦合關係。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.6 控制器"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":把職責分配給能代表以下選擇之一的類:"}]},{"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關模式"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:純虛構"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GoF:命令、外觀"}]}]},{"type":"listitem","content":[{"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":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"api層"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"根對象"}]}]},{"type":"listitem","content":[{"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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.7 多態"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":當相關選擇或行爲隨類型有所不同時,使用多態操作爲變化的行爲類型分配職責。"}]},{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關原則和模式"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:防止變異"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GoF:大量模式"}]}]}]}]},{"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/c8/c8f230c5203c6ca02bd6b41a863f26a9.png","alt":null,"title":"image.png","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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"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":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#000000","name":"user"}}],"text":"3.8 間接性"}]},{"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":"blockquote","content":[{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"解決方案"},{"type":"text","text":":將職責分配給中介對象,使其作爲其他構建或服務之間的媒介,以避免他們之間的直接耦合。"}]},{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"相關原則和模式"},{"type":"text","text":":"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GRASP:防止變異、低耦合、大量間接性中介都是純虛構"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"GoF:大量模式"}]}]}]}]},{"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}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"4.架構模式"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"除了職責分配原則,還需要一些架構模式幫助我們更好的落地。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.1 分層架構"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在分佈式系統中系統是獨立存在的,可以單獨變更而不對其他系統產生影響,但是隨着業務整體複雜度的提升也帶來了一些負面影響:"},{"type":"text","marks":[{"type":"underline"}],"text":"由於整體被分解成大量獨立的系統,隨着複雜度提升系統之間的依賴關係會變的錯綜複雜,某個系統的變更會影響其他系統,同時也會產生意想不到的問題,效率也隨之下降"},{"type":"text","text":"。這時就需要重新對分佈式系統的邏輯架構做設計,以解決系統間的不良耦合和內聚,從而提效。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分層架構"},{"type":"text","text":"是非常實用和常見的方式,TCP/IP、HTTP、操作系統等等都運用了分層,分層的本質很簡單:通過分離關注點,達到高內聚;通過向下依賴、拒絕循環依賴、使用接口,達到低耦合。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f4/f4d874d0af632beea4a6d70618ae4f89.png","alt":null,"title":"分層架構","style":[{"key":"width","value":"100%"},{"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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"分層架構也是存在缺點的"},{"type":"text","text":":按照分層架構定義消息消費應該在基礎設施層,但是消息消費是爲了執行某個業務邏輯,這樣就需要依賴應用層 或 領域層,如果真的這樣寫就會出現循環依賴問題,通過"},{"type":"text","marks":[{"type":"strong"}],"text":"依賴倒置"},{"type":"text","text":"可以解決依賴問題。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"4.2 六(多)邊形架構(洋蔥圈架構)"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"六邊形架構(Hexagonal Architecture),又稱爲"},{"type":"text","marks":[{"type":"strong"}],"text":"端口和適配器"},{"type":"text","text":"架構風格,其中的“六”具體數字沒有特殊的含義,僅僅表示一個“量級”的意思,六邊形的定義只是方便更加形象的理解。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ed/edbc06ddccf596a0cf3dd56503b96fe2.png","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}},{"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/f4/f46cc8b9907e74b55838a8551890711f.png","alt":null,"title":"圖片來源:https://www.jianshu.com/p/d3e8b9ac097b","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"5.案例"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"案例1:Jpa替換爲Mybatis"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"@Component\npublic class CloseOrderService {\n @Autowired(required = false)\n @Qualifier(\"rstOrderTransactionManager\")\n JpaTransactionManager tm;\n \n public void invalid_order(Long orderId, Long userId, Short processGroup)\n throws UserException, SystemException, UnknownException {\n //其他邏輯。。。省略\n \n // 開啓事務\n DefaultTransactionDefinition def = new DefaultTransactionDefinition();\n TransactionStatus ts = tm.getTransaction(def);\n\n try {\n order = orderDAO.get(orderId);\n order.setStatusCode(toStatus);\n order.setUpdatedAt(new Timestamp(System.currentTimeMillis()));\n orderDAO.save(order);\n //提交事務\n tm.commit(ts);\n } catch (Exception e) {\n if (!ts.isCompleted()) {\n //回滾\n tm.rollback(ts);\n }\n if (e instanceof SatisfiedStateException) {\n return;\n }\n throw e;\n }\n }\n @Transactional(transactionManager = \"rstOrderTransactionManager\", rollbackFor = Exception.class)\n public void invalidOrder(){\n }\n}\n\n@Component\npublic interface OrderDAO extends JpaRepository {\n @Query(value = \"sql語句\", nativeQuery = true)\n Long generateGlobalOrderId(@Param(\"userId\") Long userId, \n @Param(\"restaurantId\") Long restaurantId, \n @Param(\"seqName\") String seqName);\n}"}]},{"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},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"如果不出意外對Jpa的使用方式不會產生變更,意味着其相對穩定"},{"type":"text","text":",所以在當前階段來看以上耦合是正常的也不會產生負面影響。但是在以下場景會讓我們對高耦合有很明顯的體感:大家覺得Jpa不好用,想替換爲Mybatis該怎麼做?代碼中直接使用了繼承JpaRepository的OrderDAO做數據操作,由於Jpa和Mybatis的寫法不同,所以需要把使用到OrderDAO的地方都做替換:"}]},{"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":"調用OrderDAO的類(70多個類)都需要替換爲新的dao"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"使用JpaTransactionManager.getTransaction()的位置需要替換爲MyBatis的TransactionManager"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"使用@Transactional(transactionManager = \"rstOrderTransactionManager\")的位置需要改爲編寫事務提交和回滾的代碼塊兒,便於做灰度"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"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","marks":[{"type":"strong"}],"text":"結論:"},{"type":"text","text":"由於變更涉及到70多個類,同時事務管理器獲取方式也需要修改,其帶來的影響還是挺大的,不滿足“低耦合”原則,可以使用“多態”原則重新設計。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"案例2:訂單對應的支付單應該由誰來創建?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"拿餓了麼交易系統舉例,當前創建支付單的職責是由bos服務承擔(面向app的一個後端服務)的,接下我們進行分析。"}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ce/ce0d287f9a174cf29c365dd01fea61e6.png","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}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"支付單創建分爲兩種場景:"}]},{"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":"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":"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":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/2a/2a7e81ecc9869ab856c58e0bfc31f943.png","alt":null,"title":null,"style":null,"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong"}],"text":"注1"},{"type":"text","text":":如果餓了麼只會有外賣一種交易業務,當前的設計還是很穩定的,不會出現太大變化。所以識別變化點才能更好的評判當前系統設計是否合理,如:餓了麼將升級爲本地生活服務公司,根據公司戰略多少能看出我們將來不只外賣業務存在,還會有很多和本地生活相關的交易業務,這些業務會有自己的展示層(app、h5、web)同時對應會有類似bos的服務,如果有10個業務方,在支付場景就需要去對接10次,而由order做就只需要一次(支付作爲工具已經比較穩定,不會有太大變化)。"}]},{"type":"bulletedlist","content":[{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bos比order多出識別訂單結構的成本。"}]}]},{"type":"listitem","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"bos比order多出認知交易域業務知識的成本。需要深入瞭解交易狀態,這樣才知道什麼狀態才能去支付(一般是去問order服務的開發),打破了邊界。"}]}]}]}]},{"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":":bos服務不應該承擔創建支付單的職責,由order承擔最合適。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章