Design pattern to try and get data from multiple sources in a hierarchy

I have a case where I have multiple backup data sources. By data source I mean abstraction for example. File, NoSql / SQL database, Diff tables, archives. I have a specific hierarchy for data access and I can drop the data of any data source based on certain criteria. Now my question is - is there any existing design pattern to implement it? I am currently thinking of two approaches, but I think they could be improved:

for (long id : ptitleIds) {
    if(checkIfInValid(id)) {
        continue;
    }else if (getFromNdi(result, id)) {
        continue;
    } else if (getFromCimg(result,id)) {
        continue;
    } else if (getFromPtitle(result,id)) {
        continue;
    } else {
        result.put(id, EMPTY_OBJECT);
    }
}
return result;

      

Another approach I have tried is shorter, but might not be easy to understand:

for (long id : ptitleIds) {
    if(checkIfInValid(id) || getFromNdi(result, id) || getFromCimg(result,id)) || getFromPtitle(result,id)) {
        continue;
    } else {
        result.put(id, EMPTY_OBJECT);
    }
}
return result;

      

In this case, data sources are simple functions, inserting and returning true if the data is valid.

+3


source to share


1 answer


I would implement this as a kind of chain of responsibility . In the standard chain of responsibility, the next item in the chain is part of the interface for an object, but you can also implement similar functionality using decorators.

What I would do:



Define an interface for loading data ( IDataLoader

) and then run one implementation for each source. Then you have a class that concatenates 2 implementations (-> ChainingDataLoader

implements IDataLoader

), which is a decorator for the data loader that tries to load with the decorated data loader, and if that fails, it delegates to the second loader. This second bootloader can also be ChainingDataLoader

so that you can bundle as many as you need.

+1


source







All Articles