Design pattern for accessing the same data from different sources

I have this problem: I have data to save both local and remote in the cloud in order to be able to receive it from any device. Now, if there is a connection, I must download them from the remote (and, ultimately, update them if there was an update without a connection) and save any changes also locally if there is no connection that I can and should only use the local source. At first I thought about the Strategy, but this is obviously wrong than I thought about the proxy, but I do not know if it is suitable. During my project, I created managerPreferitiDB, which in a situation (possibly with a status template) gained access to localPreferitiDB (for local access) and / or cloudPreferitiDB (for remote access). I thought that I probably should have a preferitiRetriever interface,implemented by both of them, and then with two or more privileges in my proPreferitiDB proxy server.

Any tips? Is there a better sample that is suitable for this? I am using Java for Android, thanks

+4
source share
4 answers

There are no GoF templates (Gang of Four) for your scenario. GoF templates are of a lower level, while you are faced with a more complex problem, which is related to local cache and remote storage. GoF templates are not network connected.

You may find something useful in the Fowler Enterprise Application Architecture Template Catalog , such as Remote Facade and Data Transfer Object, but this is only part of the solution.

, , , GoF , .

, / , ; , .

+1

, , , . , . , , State, , , Proxy . , moblie .

0

Proxy.

-

  • , .
  • , .
  • - , -, , .

, , .

0

, , api.

-, . , , , .

-, , CloudRepository LocalRepository. , , , , . , , . , ,

. - Strategy, Proxy.

. , - . , , , .

public class Client {
    private final Repository repository;
    public Client(Repository repository) {
       this.repository repository;
    }
}

For the second problem, we need another implementation of the Repository, which I call SwitchRepository. Basically, he coordinates Cloud, Local repositories to achieve your data access goal, which depends on your Internet connection status.

public SwitchRepository implements Repository {
     private Repository cloudRepository;
     private Repository localRepoistiry;

     public SwitchRepository(Repository cloudRepository, Repository localRepository) {
        this.cloudRepository = cloudRepository;
        this.localRepository = localRepository;
    }
    public void save(Data data) {
        // do whatever we want here
        if(isInternetConnected()) {

        } else {

        }
    }

  // the same for any operations of the Repository interface
}

Summarizing:

public static void main(String[] args) {
    Repository localRepository = new LocalRepository();
    Repository cloudRepository = new CloudRepository();
    Repository switchRepository = new SwitchRepostitory(cloudRepository, localRepository);
    Client client = new Client(switchRepository);
}
0
source

Source: https://habr.com/ru/post/1620074/


All Articles