compiled repository的數(shù)據(jù)處理流程可以做到數(shù)據(jù)類型無關(guān)的(除了封裝的Result哈,ps:就是說泛型唄)。 在實(shí)踐中, 很常見的list數(shù)據(jù)處理(比如 RecyclerView中)。尤其像下面這種典型的從網(wǎng)絡(luò)下載list數(shù)據(jù)的處理流:
開發(fā)者也許想把前4步封裝成一個函數(shù)Function,這個叫做:a compiled repository,然后用一個 Updatable 作為repository的響應(yīng)實(shí)現(xiàn)第5步, 為UI提供list數(shù)據(jù)。如果大多數(shù)子程序都單獨(dú)編寫UI模型轉(zhuǎn)換方法,勢必影響代碼可讀性 (比如:將領(lǐng)域模型數(shù)據(jù)轉(zhuǎn)換成UI模型數(shù)據(jù))。
Agera 為compiled repository提供了實(shí)用工具:編譯規(guī)模較小,可重用操作符,相同風(fēng)格:
// For type clarity only, the following are smaller, reused operators:
Function<String, DataBlob> urlToBlob = …;
Function<DataBlob, List<ItemBlob>> blobToItemBlobs = …;
Predicate<ItemBlob> activeOnly = …;
Function<ItemBlob, UiModel> itemBlobToUiModel = …;
Function<List<UiModel>, List<UiModel>> sortByDateDesc = …;
Function<String, List<UiModel>> urlToUiModels =
Functions.functionFrom(String.class)
.apply(urlToBlob)
.unpack(blobToItemBlobs)
.filter(activeOnly)
.map(itemBlobToUiModel)
.morph(sortByDateDesc)
.thenLimit(5);
所謂_reused_是指 operator背后的邏輯部分在別的地方需要使用到, 并且只需要一小點(diǎn)工作就可以包裝成有用的operator接口。此外,如果超過Function/Predicate的函數(shù)定義(ps:相對復(fù)雜的函數(shù)),更要使用function compiler, 要比簡單寫一個自定義的Function好的多,已經(jīng)發(fā)生的開銷 (編譯時間:編譯附加的類的時間;運(yùn)行時時間:加載、創(chuàng)建、鏈接這些類的時間)。使用function compiler可以有效減少代碼行。
function編譯器對應(yīng)的編譯器狀態(tài),定義在FunctionCompilerStates 接口中。 就像使用 [[repository compiler|Compiled-repositories]], 編譯function的表達(dá)式是不能中間中斷的.