cherry
cherry

Reputation: 137

Design pattern to use when retrieving data from multiple sources

My questions is basically what is the ideal design pattern one should be using if we retrieve same data once from one source and sometimes from other source. The reason from multiple sources because when you are planning to move from one db to another db, One wants to keep the data of original source for sometime till the second becomes stable. So, we need to maintain two ways of retrieving data and also then formatting data accordingly in both cases.

Upvotes: 3

Views: 4163

Answers (1)

univise
univise

Reputation: 509

A proxy would be a reasonable approach - depending on your circumstances; I must stress though that there is never really an "Ideal Approach". Different designs have different advantages and disadvantages. Sometimes one approach works for your, sometimes it makes absolutly no sense. The following is thus only one possible approach which I would follow as I understand your situtation.

We have two problems - which can be phrased in two questions: How do we get the data from a source? How do we decide which source to get the data from?

First let us abstract the actual data using an interface; it will define all methods for managing and accessing the data:

public interface Data
{
    // methods for administrating the data
}

Let us use an abstraction for getting the data from a source. For this purpose we declare an interface:

public interface DataRetriever
{
    Data retrieveData();
}

We shall have two objects implementing this interface, one for source a and one for source b, respecively. After all, the client does not care how the data is obtained - the client only cares that the data is obtained:

public class RetrieverA implements DataRetriever
{
    @Override
    public Data retrieveData()
    {
        //handle obtaining the data
    }
}

public class RetrieverB implements DataRetriever
{
    @Override
    public Data retrieveData()
    {
        // handle obtaining the data
    }
}

RetrieverA and RetrieverB implement the DataRetriever interface and will implement the actual code for obtaining the data. Next let us define the actual proxy. The proxy will implement the DataRetriever interface and store a reference to RetrieverA and RetrieverB:

public class Proxy implements DataRetriever
{
    private final RetrieverA;
    private final RetrieverB;

    @Override
    public Data retrieveData()
    {
        // insert logic for deciding which source to get the data from
    }
}

The retrieveData() method will actually define the logic of when to retrieve which data from which DataRetriever. The client will get the data and can work with it, as pleased.

I hope I answered you question to the fullest extend. I advise the book of the Gang of Four, should you not have it yet (which is also my source):

http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented-ebook/dp/B000SEIBB8

Alternativly, there is a website briefly describing most patterns:

http://www.oodesign.com/

Upvotes: 7

Related Questions