gif分解合成
Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
“發現功能JavaScript”被BookAuthority評為最佳新功能編程書籍之一 !
Our natural way of dealing with complexity is to break it into smaller pieces and then put everything back together.
我們處理復雜性的自然方法是將其分解為較小的部分,然后將所有內容放回原處。
This is a two step process:
這是一個兩步過程:
- decompose the problem into smaller parts 將問題分解成更小的部分
- compose the small parts to solve the problem 組成小零件解決問題
We decompose in smaller parts because they are easier to understand and implement. The smaller parts can be developed in parallel.
我們將較小的部分分解,因為它們更易于理解和實施。 較小的部分可以并行開發。
The process of decomposition is about assigning responsibilities and giving names. This makes it easy to talk and reason about. Once we identify a responsibility, we can reuse it.
分解的過程是關于分配職責和給出名稱的。 這使得談論和推理變得容易。 一旦確定了責任,就可以重用它。
Composition is about combining the small parts together and establishing a relationship between them. We decide the way these pieces communicate, the order in which they execute, and how data flows between them.
組成是將小部分組合在一起并在它們之間建立關系。 我們決定這些部分的通信方式,它們執行的順序以及它們之間的數據流向。
We find a system hard to understand even if it is split in smaller parts, if there is a high number of relations between these parts. In order to make a system easier to understand, we need to minimize the number of possible connections between its parts.
即使這些部分之間存在大量關系,即使將系統分成較小的部分,我們也很難理解。 為了使系統更易于理解,我們需要最大程度地減少系統各部分之間可能的連接數。
對象分解 (Object decomposition)
Objects are more than state and behavior working together. Objects are things with responsibilities.
對象不僅僅是狀態和行為協同工作。 對象是有責任的事物。
分解 (Decompose)
In How to create a three layer application with React, I take a to-do list application and split the responsibilities between the following objects :
在如何使用React創建一個三層應用程序中 ,我將一個待辦事項列表應用程序并在以下對象之間劃分職責:
TodoDataService
: responsible for the communication with the server Todo APITodoDataService
:負責與服務器Todo API的通信UserDataService
: responsible for the communication with the server User API.UserDataService
:負責與服務器User API的通信。TodoStore
: the domain store for managing to-dos. It is the single source of truth regarding to-dos.TodoStore
:用于管理待辦事項的域存儲。 這是關于待辦事項的唯一事實來源。UserStore
: the domain store for managing users.UserStore
:用于管理用戶的域存儲。TodoListContainer
: the root container component displaying the list of to-dos.TodoListContainer
:顯示待辦事項列表的根容器組件。
As you can see, when decomposing, I assign responsibilities and give names.
如您所見,在分解時,我分配職責并給出名稱。
撰寫 (Compose)
Next, I compose them together in a single function. This is the place where all objects are created and dependencies injected. It is called Composition Root.
接下來,我將它們組合成一個函數。 在這里創建所有對象并注入依賴項。 它稱為合成根。
Discover Functional JavaScript was named one of the best new Functional Programming books by BookAuthority!
發現功能JavaScript被稱為 BookAuthority最好的新功能編程書籍 !
For more on applying functional programming techniques in React take a look at Functional React.
有關在React中應用函數式編程技術的更多信息,請查看 Functional React 。
Learn functional React, in a project-based way, with Functional Architecture with React and Redux.
通過帶有React和Redux的功能架構 ,以基于項目的方式學習功能性React 。
Follow on Twitter
在Twitter上關注
翻譯自: https://www.freecodecamp.org/news/how-to-make-complex-problems-easier-by-decomposing-and-composing-be57ce230c49/
gif分解合成