-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProduct.java
More file actions
58 lines (48 loc) · 1.23 KB
/
Product.java
File metadata and controls
58 lines (48 loc) · 1.23 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
46
47
48
49
50
51
52
53
54
55
56
57
58
package observer;
import java.util.ArrayList;
import java.util.List;
/**
* Concrete Implementation of the subject
* @author Khalid Elshafie
* @created 9/13/17
*/
public class Product implements Subject {
/**
* Product Name
*/
private String name;
/**
* Product avaiablity. Observers are interested in this
*/
private String availablity;
/**
* A list to hold all the observers
*/
private List<Observer> observerList;
public Product(String name) {
this.name = name;
this.observerList = new ArrayList<>();
}
@Override
public void addObserver(Observer observer) {
observerList.add(observer);
}
@Override
public void removeObserver(Observer observer) {
observerList.remove(observer);
}
@Override
public void notifyAllObservers() {
for(Observer observer: observerList) {
observer.update(availablity);
}
}
/**
* Set the product avaiablity based on boolean value and notify all observers
* @param available
*/
public void setAvailablity(boolean available) {
availablity = this.name + (available ? " Avaiable": " Not avaiable");
notifyAllObservers();
}
}