-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortfolioSubject.java
More file actions
45 lines (36 loc) · 1.11 KB
/
Copy pathPortfolioSubject.java
File metadata and controls
45 lines (36 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Control.observer_pattern;
import Entity.Asset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class PortfolioSubject {
private final String userId;
private final List<Asset> assets = new ArrayList<>();
private final List<Observer> observers = new ArrayList<>();
public PortfolioSubject(String userId) {
this.userId = userId;
}
public void addObserver(Observer obs) {
observers.add(obs);
}
public void removeObserver(Observer obs) {
observers.remove(obs);
}
private void notifyObservers(Asset asset, String action) {
for (Observer o : observers) {
o.update(userId, asset, action);
}
}
public void addAsset(Asset asset) {
assets.add(asset);
notifyObservers(asset, "added");
}
public void removeAsset(Asset asset) {
if (assets.remove(asset)) {
notifyObservers(asset, "removed");
}
}
public List<Asset> getAssets() {
return Collections.unmodifiableList(assets);
}
}