Design Pattern Considerations for Logic-Based Loading Data from Different Sources

I am currently reading about design patterns and their use. I am trying to find a design pattern that will be used to override my current code.


Load data from several different sources in a specific order into one large JavaBean. The data is loaded based on some simple initial criteria. The data returned from the first source will build criteria for the next source.

It is possible that data from some data sources is not being returned. This may not stop the loading process in the JavaBean, but based on some logic, the loading process may skip certain data sources because it does not have enough criteria to query.

Current solution:

BigJavaBean Class


Each loader class contains accessor methods for each DAO source

Requests for each DAO are generated from attributes in the BigJavaBean

There is logic in each loader class to determine if its "loaded" or not

LoaderClassDataBase for BigJavaBean

LoaderClassFlatFiles for BigJavaBean

LoaderClassXmlFiles for BigJavaBean

LoaderClassWebService for BigJavaBean

... and so on


  BigJavaBean bigJavaBean = new BigJavaBean();

  populateBigBeanMethod() {


   if (loaderClassDataBase.isLoaded()) {

       if (loaderClassFlateFile.isLoaded() {

   ... more if statements





source to share

2 answers

This is the Chain of Responsibility>

abstract class BigJavaBeanLoader {
    private final BigJavaBeanLoader successor;

    public BigJavaBeanLoader() {
        this.successor = null;

    public BigJavaBeanLoader(BigJavaBeanLoader successor) {
        this.successor = successor;

    public void loadData(BigJavaBean bean) {
        if (this.loadDataFromSource(bean) && this.successor != null) {

    protected abstract boolean loadDataFromSource(BigJavaBean bean);

    class DataBase extends BigJavaBeanLoader {
        protected boolean loadDataFromSource(BigJavaBean bean) {
            // do a database stuff
            return true;

    class FlatFiles extends BigJavaBeanLoader {
        protected boolean loadDataFromSource(BigJavaBean bean) {
            // do flat files stuff
            return true;


BigJavaBean bigJavaBean = new BigJavaBean();
BigJavaBeanLoader loader;
loader = new BigJavaBeanLoader.FlatFiles();
loader = new BigJavaBeanLoader.DataBase(loader);




For me it screams for the state pattern (there are cleaner implementations than this, but I didn't have enough coffee).

public abstract class LoaderState {
  private BigBean bean;
  public LoaderState(BigBean b) { this.bigBean = b; }
  public abstract LoaderState getNextLoader();
  public abstract void loadData();
  class DatabaseLoaderState extends LoaderState {
    public void loadData() { //do some stuff }
    public LoaderState getNextLoader() {
      if (iHadSomeData) return new FlatFileLoaderState(bigBean);
      return new SomeOtherLoaderState(bigBean); //or return null if nothing else to do


LoaderState state = new LoaderState.DatabaseLoaderState(new BigBean());
do {
  state = state.getNextLoader();
} while (state != null);




All Articles