, , 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) {
if(isInternetConnected()) {
} else {
}
}
}
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);
}
source
share