Skip to content

Commit 7c465d3

Browse files
committed
updates
1 parent 4442571 commit 7c465d3

4 files changed

Lines changed: 32 additions & 18 deletions

File tree

com.vogella.java.designpattern.observer/src/com/vogella/java/designpattern/observer/Main.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ public class Main {
77

88
public static void main(String[] args) {
99
MyModel model = new MyModel();
10+
MyObserver observer = new MyObserver(model);
1011
// We change the last name of the person, observer will get notified
1112
for (Person person : model.getPersons()) {
12-
person.setLastName(person.getLastName() + "new");
13+
person.setLastName(person.getLastName() + "1");
1314
}
14-
// We change the first name of the person, observer will get notified
15+
// We change the name of the person, observer will get notified
1516
for (Person person : model.getPersons()) {
16-
person.setLastName(person.getFirstName() + "new");
17+
person.setFirstName(person.getFirstName() + "1");
1718
}
1819
}
1920
}

com.vogella.java.designpattern.observer/src/com/vogella/java/designpattern/observer/MyModel.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.vogella.java.designpattern.observer;
22

3+
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
45
import java.util.ArrayList;
5-
import java.util.Iterator;
66
import java.util.List;
77

88
public class MyModel {
@@ -27,17 +27,16 @@ public String getFirstName() {
2727
}
2828

2929
public void setFirstName(String firstName) {
30-
this.firstName = firstName;
31-
notifyListeners();
30+
notifyListeners(this, "firstName", firstName, this.firstName = firstName);
31+
3232
}
3333

3434
public String getLastName() {
3535
return lastName;
3636
}
3737

3838
public void setLastName(String lastName) {
39-
this.lastName = lastName;
40-
notifyListeners();
39+
notifyListeners(this, "lastName", lastName, this.lastName = lastName);
4140
}
4241
}
4342

@@ -51,9 +50,9 @@ public MyModel() {
5150
persons.add(new Person("Jim", "Knopf"));
5251
}
5352

54-
private void notifyListeners() {
53+
private void notifyListeners(Object object, String property, String oldValue, String newValue) {
5554
for (PropertyChangeListener name : listener) {
56-
name.propertyChange(null);
55+
name.propertyChange(new PropertyChangeEvent(this, "firstName", oldValue, newValue));
5756
}
5857
}
5958

com.vogella.java.designpattern.observer/src/com/vogella/java/designpattern/observer/MyObserver.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import java.beans.PropertyChangeListener;
55

66
public class MyObserver implements PropertyChangeListener {
7-
public MyObserver(MyModel model) {
8-
model.addChangeListener(this);
9-
}
7+
public MyObserver(MyModel model) {
8+
model.addChangeListener(this);
9+
}
1010

11-
@Override
12-
public void propertyChange(PropertyChangeEvent arg0) {
13-
System.out.println("Things are changing...");
14-
}
15-
}
11+
@Override
12+
public void propertyChange(PropertyChangeEvent event) {
13+
System.out.println("Changed property: " + event.getPropertyName() + " old:"
14+
+ event.getOldValue() + " new: " + event.getNewValue());
15+
}
16+
}

de.vogella.junit.first/test/de/vogella/junit/first/MyClassTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import org.junit.AfterClass;
6+
import org.junit.BeforeClass;
57
import org.junit.Test;
68

79
public class MyClassTest {
810

11+
12+
@BeforeClass
13+
public static void testSetup(){
14+
// Preparation of the unit tests
15+
}
16+
17+
@AfterClass
18+
public static void testCleanup(){
19+
// Teardown for data used by the unit tests
20+
}
21+
922
@Test(expected = IllegalArgumentException.class)
1023
public void testMultiply() {
1124
MyClass tester = new MyClass();

0 commit comments

Comments
 (0)