1.源码地址
2.作用
1.让代码看起来更简洁
现在是这样创建对象的
2.解耦
我们有一个类,然后有100个地方使用它,这个时候如果我们要修改构造参数,加入一个参数,那么我们就要修改100个地方;如果过了一个月后,然后有要添加参数,这个时候就又要修改100个地方。而有了它,我们只需要修改你一个地方即可
3.
4.
5.
6.
3.使用
1.简单使用
1.依赖
根build.gradle
buildscript {
ext {
kotlin_version = "1.6.21"
koin_version = "3.2.0"
}
repositories {
mavenLocal()
mavenCentral()
google()
}
}
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app的build.gradle
implementation "io.insert-koin:koin-android:$koin_version"
2.类的实际初始化
AppMudule.kt 初始化UserRepositoryImpl(接口为UserRepository),UserPresenter(参数为UserRepository)
package com.cniao5.koin.di
import com.cniao5.koin.UserPresenter
import com.cniao5.koin.UserRepository
import com.cniao5.koin.UserRepositoryImpl
import org.koin.dsl.module
val appModule= module {
single<UserRepository> { UserRepositoryImpl() }
factory { UserPresenter(get()) }
}
详细的类内容 UserRepositoryImpl(接口为UserRepository)
package com.cniao5.koin
interface UserRepository {
fun findUser(name:String):User?
fun addUser(users:List<User>)
}
class UserRepositoryImpl :UserRepository{
private val _users= arrayListOf<User>()
override fun findUser(name: String): User? {
return _users.firstOrNull { it.name==name }
}
override fun addUser(users: List<User>) {
_users.addAll(users)
}
}
UserPresenter
package com.cniao5.koin
class UserPresenter(private val repository:UserRepository) {
fun sayHello(name:String):String{
val foundUser=repository.findUser(name)
return foundUser?.let { "hello '$it' from $this"}?:"User '$name' not found!"
}
}
3.koin的初始化与加入要使用的appModule(里面包含实际初始化的类)
MyApplication
package com.cniao5.koin
import android.app.Application
import com.cniao5.koin.data.DefaultData.DEFAULT_USERS
import com.cniao5.koin.di.appModule
import org.koin.android.ext.android.inject
import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
class MyApplication :Application() {
private val userRepository:UserRepository by inject()
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApplication)
androidLogger()
modules(appModule)
}
userRepository.addUser(DEFAULT_USERS)
}
}
UserRepositoryImpl(接口为UserRepository)
package com.cniao5.koin
interface UserRepository {
fun findUser(name:String):User?
fun addUser(users:List<User>)
}
class UserRepositoryImpl :UserRepository{
private val _users= arrayListOf<User>()
override fun findUser(name: String): User? {
return _users.firstOrNull { it.name==name }
}
override fun addUser(users: List<User>) {
_users.addAll(users)
}
}
DefaultData
package com.cniao5.koin.data
import com.cniao5.koin.User
object DefaultData {
val DEFAULT_USER=User("魂兽")
val DEFAULT_USER2=User("胡列娜","武魂殿")
val DEFAULT_USERS= listOf(DEFAULT_USER,DEFAULT_USER2)
}
4.类的初始化与类方法的调用
MainActivity
package com.cniao5.koin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.cniao5.koin.data.DefaultData
import org.koin.android.ext.android.inject
class MainActivity : AppCompatActivity() {
private val presenter: UserPresenter by inject()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val name = DefaultData.DEFAULT_USER.name
val textField=findViewById<TextView>(R.id.text)
textField.text=presenter.sayHello(name+"")
}
}
5.在ViewModel中使用
6.拓展
待修改
4.总结与问题反思
1.简单使用
2.ViewModel
3.
4.
5.
6.
|