|
| 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 | +} |
0 commit comments