2015-06-14 95 views
2

我試圖改進我的Android持久層用於多個應用程序。存儲庫模式與存儲庫工廠

是我迄今所做的是要建立一個基礎庫的抽象類,也是一個基礎庫接口,完整的代碼可以點擊這裏:https://github.com/grmaciel/android-repository-ormlite

接口:現在所有

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 DependencyRepository extends BaseRepository<Dependency> 
            implements IDependenceyRepository { 
    public DependencyRepository(Context context) { 
     super(context); 
    } 
} 

我現在想實現的目標是創建一個存儲庫工廠,允許pe德隆到不一定要建立自己的倉庫遍佈與new Instance()

的地方我所做的就是創建一個Singleton工廠必須與具有階級關係的容器進行初始化,像這樣:

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); 

所以我想知道這是一個很好的方法來幫助實現抽象嗎?我不喜歡在沒有強制要求的情況下調用工廠的初始化方法,唯一的方法是知道如果你不打電話會拋出一個我不喜歡的異常。

有沒有人能指出我正確的方向?一種改進這種設計的方法?我不想在後來的項目中發現我創建了很多強大的依賴項,並且很難改變某些內容。

任何意見,將不勝感激。

回答

3

我做了什麼來改善我的代碼,而不是重新發明輪子,我開始使用依賴注入庫(Dagger 2 - http://google.github.io/dagger/)。

您可以定義模塊,根據您的需求將您的需求存儲庫返回到應用程序或活動中。