| 81 | |
| 82 | = Code Reuse = |
| 83 | |
| 84 | Do not have the same functionality on more than one place - this increases the chances of mistakes and makes it difficult for refactor. |
| 85 | |
| 86 | In the class !BookView we had the following two methods: |
| 87 | As we see we have code reuse, and the private method do not register the view as observer of the page. |
| 88 | {{{ |
| 89 | @Override |
| 90 | public void pageAdded(Page page) { |
| 91 | PageView pageView = new PageView(this, page); |
| 92 | this.pageViews.add(pageView); |
| 93 | page.getObservable().addObserver(pageView); |
| 94 | this.observable.getInvoker().pageViewCreated(pageView); |
| 95 | } |
| 96 | |
| 97 | ... |
| 98 | |
| 99 | private void createChildView(Page page){ |
| 100 | PageView p = new PageView(this, page); |
| 101 | this.pageViews.add(p); |
| 102 | this.observable.getInvoker().pageViewCreated(p); |
| 103 | } |
| 104 | }}} |
| 105 | |
| 106 | Shall be: |
| 107 | |
| 108 | {{{ |
| 109 | private void createChildView(Page page){ |
| 110 | final PageView p = new PageView(this, page); |
| 111 | this.pageViews.add(p); |
| 112 | this.observable.getInvoker().pageViewCreated(p); |
| 113 | page.getObservable().addObserver(p); |
| 114 | } |
| 115 | |
| 116 | ... |
| 117 | public void pageAdded(Page page) { |
| 118 | createChildView(page); |
| 119 | } |
| 120 | }}} |