Student_Answers_Chapter_14.doc_第1頁
Student_Answers_Chapter_14.doc_第2頁
Student_Answers_Chapter_14.doc_第3頁
Student_Answers_Chapter_14.doc_第4頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

Suggested answer for Exercise 14.2A simple-minded implementation of the structure in Figure 14.8 is given below. It is apparent from the code that the abstract supplier interface has not been modified when the new abstract supplier interface was added, and hence that the module is closed. public interface AbstractSupplier public void operation() ;public interface NewAbsSupplier extends AbstractSupplier public void newOperation() ;public class ConcreteSupplier implements AbstractSupplier public void operation() System.out.println(ConcreteSupplier:operation() called) ; public class NewConcSupplier implements NewAbsSupplier public void operation() System.out.println(NewConcSupplier:operation() called) ; public void newOperation() System.out.println(NewConcSupplier:newOperation() called) ; public class Client AbstractSupplier supplier ; public Client(AbstractSupplier s) supplier = s ; public void doOperation() supplier.operation() ; public class NewClient NewAbsSupplier supplier ; public NewClient(NewAbsSupplier s) supplier = s ; public void doOperation() supplier.operation() ; public void doNewOperation() supplier.newOperation() ; Suggested answer for Exercise 14.5Observer Document/View class Observer class CView class ConcreteObserver class CMyView Observer.Update() CView.OnDraw() class Subject class CDocument class ConcreteSubject class CMyDoc Subject.Attach() CSubject.AddView() Subject.Detach() CSubject.RemoveView() Subject.Notify() CSubject.UpdateAllViews() Subject.SetState() CSubject.SetStateInfo() Subject.GetState() CSubject.GetStateInfo() This table shows that virtually every feature of Observer can be mapped into a corresponding feature of Document/View. There are two significant differences between the two patterns. Concrete observers hold a reference to their subjects, whereas in Document/View all references between views and documents are maintained at the abstract level. In practice, this is not very significant, and it can be argued that Document/View supports the association in Observer, as it is simply inherited from the bidirectional association linking the abstract classes. Another difference is the complexity of the update operation in Document/View. In Observer, the Notify() operation in the subject calls Update() in the observer, which in turn calls GetState(). In Document view, notification calls the OnUpdate() function, whereas it is a different function, OnDraw() which retrieves the document state. Again, however, it can be argued that Document/View does support the interaction defined in Observer, albeit in a not immediately straightforward way. There are therefore strong grounds for regarding Document/View as an instantiation of the Observer pattern. Suggested answer for Exercise 14.6This question should refer to the document/view architecture in addition to MVC. Figure 14.18 shows an MVC interaction where notification takes place every time the state of the subject (here the model) changes. In the document/view architecture, one option would be to have the operation CMyDoc:SetStateInfo() call OnUpdate() every time it are called. This approach has the advantage that client objects which are updating the document dont have to remember to call the notification operation, and the views are guaranteed to be automatically and promptly made consistent with the document or model state. On the other hand, if a number of consecutive updates are applied to a model, each will cause the corresponding views to be updated independently of the others. It may be sufficient to update the views once, at the end of this sequence of updates, and updating more frequently may be unacceptably inefficient. The alternative would be for objects which update the document to call UpdateAllViews() in the document/view architecture when the update is finished. This allows updates to wait until a sequence of changes has been made, but places an additional responsibility on client objects, thus complicating their implementation and

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論