forked from iluwatar/java-design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextView.java
44 lines (38 loc) · 1.02 KB
/
TextView.java
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
package com.iluwatar.binding;
public class TextView implements Observer<String> {
private String text;
private ObservableProperty<String> boundProperty;
@Override
public void update(String newValue) {
// Prevent unnecessary updates
if (text != null && text.equals(newValue)) {
return;
}
this.text = newValue;
System.out.println("TextView updated: " + text);
}
@Override
public void bind(ObservableProperty<String> observableProperty) {
// Unbind from the current property if already bound
if (this.boundProperty != null) {
this.boundProperty.removeObserver(this);
}
this.boundProperty = observableProperty;
}
@Override
public void unbind() {
if (this.boundProperty != null) {
this.boundProperty.removeObserver(this);
this.boundProperty = null;
}
}
public void setText(String newText) {
if (boundProperty != null) {
boundProperty.setValue(newText);
}
this.text = newText;
}
public String getText() {
return text;
}
}