我目前正在将我的项目架构从MVP转换为MVVM。当我在处理它时,我发现有些东西使我感到困惑:
在项目iosched的ScheduleViewModelFactory.kt
中,工厂实现 ViewModelProvider.Factory:
class ScheduleViewModelFactory(
private val userEventRepository:DefaultSessionAndUserEventRepository
) : ViewModelProvider.Factory {
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(ScheduleViewModel::class.java)) {
return ScheduleViewModel(LoadUserSessionsByDayUseCase(userEventRepository)) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
在codelab中的Sunshine项目的
DetailViewModelFactory.java
中,工厂扩展了 ViewModelProvider.NewInstanceFactory:public class DetailViewModelFactory extends ViewModelProvider.NewInstanceFactory {
private final SunshineRepository mRepository;
private final Date mDate;
public DetailViewModelFactory(SunshineRepository repository, Date date) {
this.mRepository = repository;
this.mDate = date;
}
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
//noinspection unchecked
return (T) new DetailActivityViewModel(mRepository, mDate);
}
}
我想知道:
最佳答案
基于 ViewModelProvider 文档:
public class ViewModelProvider {
/**
* Implementations of {@code Factory} interface are responsible to instantiate ViewModels.
*/
public interface Factory {
@NonNull
<T extends ViewModel> T create(@NonNull Class<T> modelClass);
(...)
/**
* Simple factory, which calls empty constructor on the given class.
*/
public static class NewInstanceFactory implements Factory {
@SuppressWarnings("ClassNewInstance")
@NonNull
@Override
public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
//noinspection TryWithIdenticalCatches
try {
return modelClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
} catch (IllegalAccessException e) {
throw new RuntimeException("Cannot create an instance of " + modelClass, e);
}
}
}
(...)
}
并考虑到
newInstance()
的geeksforgeeks描述:我假设
NewInstanceFactory
是Factory
的实现,可以在我们要创建不同类型的ViewModel时使用。另一方面,在Google的android-architecture/todoapp中有:
public class ViewModelFactory extends ViewModelProvider.NewInstanceFactory {
(...)
@Override
public <T extends ViewModel> T create(Class<T> modelClass) {
if (modelClass.isAssignableFrom(StatisticsViewModel.class)) {
//noinspection unchecked
return (T) new StatisticsViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(TaskDetailViewModel.class)) {
//noinspection unchecked
return (T) new TaskDetailViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(AddEditTaskViewModel.class)) {
//noinspection unchecked
return (T) new AddEditTaskViewModel(mApplication, mTasksRepository);
} else if (modelClass.isAssignableFrom(TasksViewModel.class)) {
//noinspection unchecked
return (T) new TasksViewModel(mApplication, mTasksRepository);
}
throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
}
}
他们正在使用
NewInstanceFactory
,但是覆盖了create
方法! 据我所知,如果我们覆盖它,则与使用常规Factory
没什么区别。关于android - ViewModelProvider.Factory和ViewModelProvider.NewInstanceFactory有什么区别?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52467801/