Skip to content

Commit 68166ed

Browse files
observer pattern
1 parent fc6e5e5 commit 68166ed

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package learnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
public interface CoreFamily {
4+
public void registerRelative(Relative relative);
5+
public void removeRelative(Relative relative);
6+
public void notifyRelative();
7+
public void setCoreFamilyAttributes(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package learnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class CoreFamilyImpl implements CoreFamily {
7+
8+
private String coreFamilyName;
9+
10+
private String bibleReadingProgress;
11+
12+
private int monthsWeMarried;
13+
14+
private int timesWeHiked;
15+
16+
private List<Relative> relatives;
17+
18+
public CoreFamilyImpl(String bibleReadingProgress, String coreFamilyName, int monthsWeMarried, int timesWeHiked) {
19+
this.relatives = new ArrayList<Relative>();
20+
this.bibleReadingProgress = bibleReadingProgress;
21+
this.coreFamilyName = coreFamilyName;
22+
this.monthsWeMarried = monthsWeMarried;
23+
this.timesWeHiked = timesWeHiked;
24+
}
25+
26+
@Override
27+
public void registerRelative(Relative relative) {
28+
relatives.add(relative);
29+
}
30+
31+
@Override
32+
public void removeRelative(Relative relative) {
33+
int i = relatives.indexOf(relative);
34+
if(i > 0){
35+
relatives.remove(i);
36+
}
37+
}
38+
39+
@Override
40+
public void notifyRelative() {
41+
for(int i = 0; i < relatives.size(); i++){
42+
Relative relative = (Relative) relatives.get(i);
43+
relative.update(coreFamilyName, timesWeHiked, monthsWeMarried, bibleReadingProgress);
44+
}
45+
}
46+
47+
public void hasUpdates(){
48+
notifyRelative();
49+
}
50+
51+
@Override
52+
public void setCoreFamilyAttributes(String coreFamilyNames,
53+
int hikingTimes, int marriedMonths, String bibleReadingProgress) {
54+
this.coreFamilyName = coreFamilyNames;
55+
this.timesWeHiked = hikingTimes;
56+
this.monthsWeMarried = marriedMonths;
57+
this.bibleReadingProgress = bibleReadingProgress;
58+
hasUpdates();
59+
}
60+
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package learnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
/**NOTE: I didn't really implement the Observer pattern from Head First book here.
4+
* Just implemented the introduction part here. Could continue if interests arise in the future. - 10/04/2015*/
5+
6+
/**
7+
* The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
8+
*
9+
* Subjects + Observers = Observer Pattern */
10+
11+
/**Design principle:
12+
* Strive for loosely coupled designs between objects that interact.*/
13+
public class ObserverPatternTestDrive {
14+
15+
public static void main(String[] args) {
16+
CoreFamily coreFamily = new CoreFamilyImpl(" 1 Peter ", "Steve Sun & Sophie Yan", 1, 7);
17+
Relative eason = new RelativeImpl("Eason", coreFamily);
18+
coreFamily.registerRelative(eason);
19+
Relative motherInLaw = new RelativeImpl("Jinzhi Lin", coreFamily);
20+
coreFamily.registerRelative(motherInLaw);
21+
Relative fatherInLaw = new RelativeImpl("Jianlin Yan", coreFamily);
22+
coreFamily.registerRelative(fatherInLaw);
23+
24+
coreFamily.setCoreFamilyAttributes("Sophie and Steve NEW NAME", 8, 2, "1 John");
25+
System.out.print("Program ended.");
26+
}
27+
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package learnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
public interface Relative {
4+
public void update(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress);
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package learnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
public class RelativeImpl implements Relative {
4+
5+
private String relativeName;
6+
7+
private CoreFamily coreFamily;
8+
9+
private String coreFamilyName;
10+
11+
private String bibleReadingProgress;
12+
13+
private int monthsWeMarried;
14+
15+
private int timesWeHiked;
16+
17+
public RelativeImpl(String relativeName, CoreFamily coreFamily) {
18+
this.relativeName = relativeName;
19+
this.coreFamily = coreFamily;
20+
}
21+
22+
@Override
23+
public void update(String coreFamilyNames, int hikingTimes, int marriedMonths, String bibleReadingProgress) {
24+
System.out.print("Before updating:\n");
25+
display();
26+
this.bibleReadingProgress = bibleReadingProgress;
27+
this.coreFamilyName = coreFamilyNames;
28+
this.monthsWeMarried = marriedMonths;
29+
this.timesWeHiked = hikingTimes;
30+
System.out.print("After updating:\n");
31+
display();
32+
}
33+
34+
private void display() {
35+
System.out.println("Current CoreFamilyAttributes are, coreFamilyName is: " + coreFamilyName + "\tbibleReadingProgress is: "
36+
+ bibleReadingProgress + "\tmonthsWeMarried is: " + monthsWeMarried + "\ttimesWeHiked is: " + timesWeHiked);
37+
}
38+
39+
}

0 commit comments

Comments
 (0)