Android 簡單服務(wù)定位器模式實(shí)現(xiàn)
依賴注入(Dependency Injection)和服務(wù)定位器(Service Locator)是實(shí)現(xiàn)控制反轉(zhuǎn)(Inversion of Control)的兩種主要手段。
Android的主流依賴注入框架有:Dagger 和 Kion
這些依賴注入框架都感覺比較重。
服務(wù)定位器比如少見,這里提供一個一個簡單的服務(wù)定位器模式實(shí)現(xiàn)。
引入
項(xiàng)目地址:github.com/czy1121/ser…
repositories {
maven { url "https://gitee.com/ezy/repo/raw/android_public/"}
}
dependencies {
implementation "me.reezy.jetpack:servicelocator:0.4.0"
}
API
// 獲取實(shí)例 inline fun <reified T> resolve(name: String = T::class.java.name): T? // 注冊為單例 inline fun <reified T> singleton(name: String = T::class.java.name, crossinline block: () -> T) // 注冊為工廠 inline fun <reified T> factory(name: String = T::class.java.name, crossinline block: () -> T)
使用
單例,每次resolve獲得的都是同一實(shí)例
class SomeService {
fun doSomething() {
}
}
// 注冊
singleton {
SomeService()
}
// 獲取
val service = resolve<SomeService>()
具名單例
class NamedService(val name: String) {
fun doSomething() {
}
}
// 注冊
singleton("a") {
NamedService("aaa")
}
singleton("b") {
NamedService("bbb")
}
// 獲取
val serviceA = resolve<NamedService>("a")
val serviceB = resolve<NamedService>("b")
工廠,每次resolve都會產(chǎn)生新實(shí)例
class SomeService {
fun doSomething() {
}
}
// 注冊
factory {
SomeService()
}
// 獲取,每次resolve都會產(chǎn)生新實(shí)例
val service1 = resolve<SomeService>()
val service2 = resolve<SomeService>()
具名工廠
class NamedService(val name: String) {
fun doSomething() {
}
}
// 注冊
factory("a") {
NamedService("aaa")
}
factory("b") {
NamedService("bbb")
}
// 獲取
// A1 與 A2 是使用同一工廠產(chǎn)生的不同實(shí)例
// A1 與 B1 是使用不同工廠產(chǎn)生的不同實(shí)例
val serviceA1 = resolve<NamedService>("a")
val serviceA2 = resolve<NamedService>("a")
val serviceB1 = resolve<NamedService>("b")
val serviceB2 = resolve<NamedService>("b")
以上就是Android 簡單服務(wù)定位器模式實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于Android 簡單服務(wù)定位器模式的資料請關(guān)注腳本之家其它相關(guān)文章!
- Android制作一個錨點(diǎn)定位的ScrollView
- android加密參數(shù)定位實(shí)現(xiàn)方法
- Android Studio使用Profiler來完成內(nèi)存泄漏的定位
- 解決Android原生定位的坑
- Android實(shí)現(xiàn)點(diǎn)擊某個按鈕指定位置彈出布局
- Android RecycleView滑動停止后自動吸附效果的實(shí)現(xiàn)代碼(滑動定位)
- Android 百度地圖定位實(shí)現(xiàn)仿釘釘簽到打卡功能的完整代碼
- android studio 使用Mocklocation虛擬定位
- 解決Android 10/Android Q手機(jī)在后臺無法正常定位問題
- Android實(shí)現(xiàn)高德地圖顯示及定位
- Android如何實(shí)現(xiàn)模擬定位
相關(guān)文章
Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取新浪天氣預(yù)報(bào)數(shù)據(jù)的方法,涉及Android基于新浪接口的調(diào)用及數(shù)據(jù)處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android自定義實(shí)現(xiàn)一個車牌字母選擇鍵盤
這篇文章主要為大家詳細(xì)介紹了Android如何自定義實(shí)現(xiàn)一個車牌字母選擇鍵盤,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
Android自定義View實(shí)現(xiàn)QQ消息氣泡
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)QQ消息氣泡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
android h5頁面獲取不到定位數(shù)據(jù)的問題解決
我們經(jīng)常會遇到onGeolocationPermissionsShowPrompt 已經(jīng)執(zhí)行,但仍然沒有獲取到定位數(shù)據(jù)的問題,所以本文給大家介紹了android h5頁面獲取不到定位數(shù)據(jù)的問題解決,需要的朋友可以參考下2024-11-11
Android自定義popupwindow實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android系統(tǒng)進(jìn)程間通信(IPC)機(jī)制Binder中的Client獲得Server遠(yuǎn)程接口過程源代碼分析
本文主要介紹Android 通信Binder中的Client獲得Server遠(yuǎn)程接口,這里對Android Binder 中Client中Server 源碼做了詳細(xì)分析介紹,有研究Android源碼的小伙伴可以參考下2016-08-08
Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例
這篇文章主要介紹了Android接入支付寶實(shí)現(xiàn)支付功能實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

