Java 带有存储库工厂的存储库模式

Java 带有存储库工厂的存储库模式,java,android,repository,persistence,repository-pattern,Java,Android,Repository,Persistence,Repository Pattern,我正在努力改进我的Android持久层,以便在多个应用程序中使用 到目前为止,我所做的是设置一个基本存储库抽象类和一个基本存储库接口,完整的代码可以在这里查看: 接口: public interface IRepository<T, Id> { public void save(T entity) throws SQLException; public void saveBatch(List<T> entities) throws Exception;

我正在努力改进我的Android持久层,以便在多个应用程序中使用

到目前为止,我所做的是设置一个基本存储库抽象类和一个基本存储库接口,完整的代码可以在这里查看:

接口:

public interface IRepository<T, Id> {
    public void save(T entity) throws SQLException;
    public void saveBatch(List<T> entities) throws Exception;
    public List<T> queryAll() throws SQLException;
    public T findById(Id id) throws SQLException;
    public void delete(T entity) throws SQLException;
}
工厂:

public class RepositoryFactory {
    private Map<Object, Object> repositories = new HashMap<>();
    private final String LOG_TAG = RepositoryFactory.class.getSimpleName();
    private Context context;
    private static RepositoryFactory instance;
    private BaseRepositoryContainer container;

    private RepositoryFactory() {}

    public void init(BaseRepositoryContainer container) {
        this.container = container;
        this.context = container.getContext();
        this.configureRepositories(container.getRepositoriesMap());
    }

    private <T extends IRepository> void configureRepositories(Map<Class<T>, Class<T>> repositoriesMap) {
        for (Entry<Class<T>, Class<T>> entry : repositoriesMap.entrySet()) {
            this.registerRepository(entry.getKey(), entry.getValue());
        }

    }

    private <T extends IRepository> void registerRepository(Class<T> repInterface, Class<T> realRepository) {
        repositories.put(repInterface, this.createRepository(realRepository));
    }

    public <T extends IRepository> T getRepository(Class<T> repInterface) {
        if (container == null) {
            throw new UnsupportedOperationException("You should call init method providing a container.");
        }

        return (T) repositories.get(repInterface);
    }

    private <T extends IRepository> T createRepository(Class<T> repoClass) {
        try {
            T instance = repoClass.getConstructor(Context.class).newInstance(context);
            Log.d(LOG_TAG, "Repository " + repoClass.getSimpleName() + " created");
            return instance;
        } catch (InstantiationException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (IllegalAccessException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (InvocationTargetException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (NoSuchMethodException e) {
            Log.d(LOG_TAG, e.toString());
        }

        return null;
    }

    public static RepositoryFactory getInstance() {
        if (instance == null) {
            instance = new RepositoryFactory();
        }

        return instance;
    }
}
所以我想知道这是否是一种帮助实现抽象的好方法?我不太喜欢调用工厂的init方法而不强迫人们这样做,唯一知道的方法是如果你不调用,就会抛出一个我不喜欢的异常

有人能给我指出正确的方向吗?一种改进这种设计的方法?我不想在以后的项目中发现,我已经创建了很多强大的依赖关系,并且很难改变某些东西


任何建议都将不胜感激。

我所做的是改进代码,而不是重新发明我开始使用依赖注入库(Dagger 2-)的轮子

根据您的需要,您可以定义在应用程序或活动中返回所需存储库的模块

public abstract class BaseRepositoryContainer {
    private final Context context;

    public BaseRepositoryContainer(Context context) {
        this.context = context;
    }

    public abstract <T extends IRepository> Map<Class<T>, Class<T>> getRepositoriesMap();

    public Context getContext() {
        return context;
    }
}
public class RepositoryFactory {
    private Map<Object, Object> repositories = new HashMap<>();
    private final String LOG_TAG = RepositoryFactory.class.getSimpleName();
    private Context context;
    private static RepositoryFactory instance;
    private BaseRepositoryContainer container;

    private RepositoryFactory() {}

    public void init(BaseRepositoryContainer container) {
        this.container = container;
        this.context = container.getContext();
        this.configureRepositories(container.getRepositoriesMap());
    }

    private <T extends IRepository> void configureRepositories(Map<Class<T>, Class<T>> repositoriesMap) {
        for (Entry<Class<T>, Class<T>> entry : repositoriesMap.entrySet()) {
            this.registerRepository(entry.getKey(), entry.getValue());
        }

    }

    private <T extends IRepository> void registerRepository(Class<T> repInterface, Class<T> realRepository) {
        repositories.put(repInterface, this.createRepository(realRepository));
    }

    public <T extends IRepository> T getRepository(Class<T> repInterface) {
        if (container == null) {
            throw new UnsupportedOperationException("You should call init method providing a container.");
        }

        return (T) repositories.get(repInterface);
    }

    private <T extends IRepository> T createRepository(Class<T> repoClass) {
        try {
            T instance = repoClass.getConstructor(Context.class).newInstance(context);
            Log.d(LOG_TAG, "Repository " + repoClass.getSimpleName() + " created");
            return instance;
        } catch (InstantiationException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (IllegalAccessException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (InvocationTargetException e) {
            Log.d(LOG_TAG, e.toString());
        } catch (NoSuchMethodException e) {
            Log.d(LOG_TAG, e.toString());
        }

        return null;
    }

    public static RepositoryFactory getInstance() {
        if (instance == null) {
            instance = new RepositoryFactory();
        }

        return instance;
    }
}
// when the application is first run
RepositoryFactory.getInstance().init(new RepositoryContainer(this));
// retreaving the repository
IDependenceyRepository repository = RepositoryFactory.getInstance()
                .getRepository(IDependenceyRepository.class);