这个问题是关于 OOP(类/接口(interface))设计的。
我正在开发一个 android 库,而不是一个应用程序。
该应用程序将使用此库。
这个库是由 Repository 模式开发的。
一个存储库和 2 个数据源(本地、远程)。
因为本地数据源使用“SharedPreference”,所以需要Context。
下面是我的存储库接口(interface)和实现。
interface MyRepository {
fun create(String data)
fun get(key: String): String
}
class MyRepositoryImpl(
private val localDataSource: LocalDataSource,
private val remoteDataSource: RemoteDataSource
): MyRepository {
fun create(String data) {
localDataSource.create(data);
remoteDataSource.create(data);
}
fun get(key: String): String {
// temp code
return localDataSource.get(key)
}
companion object {
private var instance: MyRepository? = null
fun getInstance(context: Context): MyRepository {
if (instance == null) {
val localDataSource: LocalDataSource = LocalDataSourceImpl.getInstance(context)
val remoteDataSource: RemoteDataSource = RemoteDataSourceImpl.getInstance()
instance = MyRepositoryImpl(localDataSource, remoteDataSource)
}
return instance!!
}
}
}
MyRepositoryImpl 由 Singleton 模式实现。
因为它应该在应用程序的任何地方使用。
因此,应用程序开发人员应该能够获取 MyRepository 的实例,例如:
val myRepository = MyRepositoryImpl.getInstance(context)
val data = myRepository.get("key")
但它看起来很奇怪......“getInstance(context)”。
我认为这不是一个好方法。
请问还有更智能的设计吗?
最佳答案
在 Kotlin 中,您可以使用 对象 关键字以线程安全的方式实现单例模式。无需在伴生对象中定义 getInstance 方法。只需定义您的 MyRepository
类作为对象,如下所示。通过重载调用运算符,您将能够传递对初始化 localDataSource 和 remoteDataSource 实例有用的上下文。
object MyRepository {
private lateinit var localDataSource: LocalDataSource
private lateinit var remoteDataSource: RemoteDataSource
operator fun invoke(context: Context): MyRepository {
//...
localDataSource = LocalDataSourceImpl.getInstance(context)
remoteDataSource = RemoteDataSourceImpl.getInstance(context)
return this
}
}
此代码将编译为以下 java 代码(静态初始化 block ):
public final class MyRepository {
public static final MyRepository INSTANCE;
private SomeSingleton() {
INSTANCE = (MyRepository) this;
}
static {
new MyRepository();
}
}
这样,您就可以像普通的非对象类一样获得 MyRepository 类的实例,只需执行以下操作:
val repo = MyRepository(this) // this is your context
您在
MyRepository
中定义的每个方法类将像 java 静态方法一样可访问,因此您可以像这样调用它:MyRepository.myMethod()
更多详情 here
关于oop - 安卓 : repository pattern what is the best solution to create an object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55371861/