Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit68166ed

Browse files
observer pattern
1 parentfc6e5e5 commit68166ed

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
publicinterfaceCoreFamily {
4+
publicvoidregisterRelative(Relativerelative);
5+
publicvoidremoveRelative(Relativerelative);
6+
publicvoidnotifyRelative();
7+
publicvoidsetCoreFamilyAttributes(StringcoreFamilyNames,inthikingTimes,intmarriedMonths,StringbibleReadingProgress);
8+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.List;
5+
6+
publicclassCoreFamilyImplimplementsCoreFamily {
7+
8+
privateStringcoreFamilyName;
9+
10+
privateStringbibleReadingProgress;
11+
12+
privateintmonthsWeMarried;
13+
14+
privateinttimesWeHiked;
15+
16+
privateList<Relative>relatives;
17+
18+
publicCoreFamilyImpl(StringbibleReadingProgress,StringcoreFamilyName,intmonthsWeMarried,inttimesWeHiked) {
19+
this.relatives =newArrayList<Relative>();
20+
this.bibleReadingProgress =bibleReadingProgress;
21+
this.coreFamilyName =coreFamilyName;
22+
this.monthsWeMarried =monthsWeMarried;
23+
this.timesWeHiked =timesWeHiked;
24+
}
25+
26+
@Override
27+
publicvoidregisterRelative(Relativerelative) {
28+
relatives.add(relative);
29+
}
30+
31+
@Override
32+
publicvoidremoveRelative(Relativerelative) {
33+
inti =relatives.indexOf(relative);
34+
if(i >0){
35+
relatives.remove(i);
36+
}
37+
}
38+
39+
@Override
40+
publicvoidnotifyRelative() {
41+
for(inti =0;i <relatives.size();i++){
42+
Relativerelative = (Relative)relatives.get(i);
43+
relative.update(coreFamilyName,timesWeHiked,monthsWeMarried,bibleReadingProgress);
44+
}
45+
}
46+
47+
publicvoidhasUpdates(){
48+
notifyRelative();
49+
}
50+
51+
@Override
52+
publicvoidsetCoreFamilyAttributes(StringcoreFamilyNames,
53+
inthikingTimes,intmarriedMonths,StringbibleReadingProgress) {
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+
packagelearnHeadFirstDesignPatterns.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+
publicclassObserverPatternTestDrive {
14+
15+
publicstaticvoidmain(String[]args) {
16+
CoreFamilycoreFamily =newCoreFamilyImpl(" 1 Peter ","Steve Sun & Sophie Yan",1,7);
17+
Relativeeason =newRelativeImpl("Eason",coreFamily);
18+
coreFamily.registerRelative(eason);
19+
RelativemotherInLaw =newRelativeImpl("Jinzhi Lin",coreFamily);
20+
coreFamily.registerRelative(motherInLaw);
21+
RelativefatherInLaw =newRelativeImpl("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+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
publicinterfaceRelative {
4+
publicvoidupdate(StringcoreFamilyNames,inthikingTimes,intmarriedMonths,StringbibleReadingProgress);
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
packagelearnHeadFirstDesignPatterns.chapter_2_observer_pattern;
2+
3+
publicclassRelativeImplimplementsRelative {
4+
5+
privateStringrelativeName;
6+
7+
privateCoreFamilycoreFamily;
8+
9+
privateStringcoreFamilyName;
10+
11+
privateStringbibleReadingProgress;
12+
13+
privateintmonthsWeMarried;
14+
15+
privateinttimesWeHiked;
16+
17+
publicRelativeImpl(StringrelativeName,CoreFamilycoreFamily) {
18+
this.relativeName =relativeName;
19+
this.coreFamily =coreFamily;
20+
}
21+
22+
@Override
23+
publicvoidupdate(StringcoreFamilyNames,inthikingTimes,intmarriedMonths,StringbibleReadingProgress) {
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+
privatevoiddisplay() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp