I am starting to take reactive programming a little more, and I'm trying to apply it to typical business tasks. One template that I often develop is database-based classes. I have a specific specific class of units, for example ActionProfile, whose instances are controlled by using ActionProfileManagerwhich creates instances from the database table and stores them in Map<Integer,ActionProfile>, where Integeris the key actionProfileId. ActionProfileManagercan periodically clean and re-import data and notify all dependencies in order to re-extract it from its card.
public final class ActionProfileManager {
private volatile ImmutableMap<Integer,ActionProfile> actionProfiles;
private ActionProfileManager() {
this.actionProfiles = importFromDb();
}
public void refresh() {
this.actionProfiles = importFromDb();
notifyEventBus();
}
public ActionProfile forKey(int actionProfileId) {
return actionProfiles.get(actionProfiles);
}
private ImmutableMap<Integer,ActionProfile> importFromDb() {
return ImmutableMap.of();
}
private void notifyEventBus() {
}
}
, , , . , , - Map , . , rxjava-jdbc . - .
public final class ActionProfileManager {
private final BehaviorSubject<ImmutableMap<Integer,ActionProfile>> actionProfiles;
private ActionProfileManager() {
this.actionProfiles = BehaviorSubject.create(importFromDb());
}
public void refresh() {
actionProfiles.onNext(importFromDb());
}
public Observable<ActionProfile> forKey(int actionProfileId) {
return actionProfiles.map(m -> m.get(actionProfileId));
}
private ImmutableMap<Integer,ActionProfile> importFromDb() {
return ImmutableMap.of();
}
}
, , Observable<ActionProfile> .
public final class ActionProfileManager {
private final ReplaySubject<ActionProfile> actionProfiles;
private ActionProfileManager() {
this.actionProfiles = ReplaySubject.create();
importFromDb();
}
public void refresh() {
importFromDb();
}
public Observable<ActionProfile> forKey(int actionProfileId) {
return actionProfiles.filter(m -> m.getActionProfileID() == actionProfileId).last();
}
private void importFromDb() {
}
}
? , GC'd? ?
, ? , ?