我目前正在将我的项目架构从MVP转换为MVVM。当我在处理它时,我发现有些东西使我感到困惑:

在项目ioschedScheduleViewModelFactory.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.Factory ViewModelProvider.NewInstanceFactory 有什么区别?
  • 为什么像上述代码一样使用它们?
  • 使用它们中的每一种的最佳实践/方案是什么?
  • 最佳答案



    基于 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描述:



    我假设NewInstanceFactoryFactory的实现,可以在我们要创建不同类型的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/

    10-09 07:04