16 Observer Pattern

834 views
703 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
834
On SlideShare
0
From Embeds
0
Number of Embeds
17
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

16 Observer Pattern

  1. 1. TK2023 Object-Oriented Software Engineering CHAPTER 16 Observer Pattern
  2. 2. INTRODUCTION <ul><li>In the Process Sale use case text, the System should display item description and price of item when the enterItem () system event occurs. </li></ul><ul><ul><ul><li>System records sale line item and presents item description and price . Price calculated from a set of price rules. </li></ul></ul></ul><ul><li>However, due to Model-View Separation , we have deferred making a decision as to how the output task should be designed. </li></ul>
  3. 3. DESIGN 1 : Register : Sale sli : SalesLineItem { new } 2: makeLineItem(pd[i],qty) 2.1: SalesLineItem(pd[i],qty) 2.2: add(sli) : ProductCatalog 1: pd[i] = getProductDesc(itemID) 1.1: pd[i] = find(itemID) pd[i] : ProductDescription {new} : SalesWindow enterItem(itemID, qty)) 5: dispDetails(desc,price) 3: desc = getDesc() 4: price = getPrice () : ArrayList<SalesLineItem> pd : ArrayList<ProductDescription>
  4. 4. <ul><li>Why should we not choose Design 1? </li></ul><ul><li>It violates Model-View Separation . Register (a domain object) is coupled to a UI object ( SalesWindow ). In Model-View Separation , “model” objects should not know about “view” objects (such as SalesWindow ). </li></ul><ul><li>A consequence of Model-View Separation is that the “view” objects in the UI layer can be replaced with new ones without impacting non-UI objects. </li></ul>
  5. 5. THE OBSERVER PATTERN <ul><li>Problem: </li></ul><ul><ul><ul><li>Different kinds of observer objects are interested in the state changes or events of a publisher object, and want to react in their own way when the publisher generates an event. Moreover, the publisher wants to maintain low coupling to the observers. What to do? </li></ul></ul></ul>
  6. 6. <ul><li>Solution: </li></ul><ul><ul><ul><li>Define an “observer” interface. Observers need to implement this interface. The publisher can dynamically register observers who are interested in an event and notify them when an event occurs. </li></ul></ul></ul>
  7. 7. EXAMPLE <ul><li>Consider the following design: </li></ul>
  8. 8. <ul><li>This design violates Model-View Separation . Problem: </li></ul><ul><ul><ul><ul><li>How could the Timekeeper object inform the UI objects that a time change has occurred? </li></ul></ul></ul></ul><ul><li>The Timekeeper object needs to communicate indirectly with the UI objects… use the Observer pattern. </li></ul>
  9. 10. <ul><li>The Timekeeper object is associated with an Observable object. </li></ul><ul><li>The Observable object is responsible for maintaining a set of Observer objects who are interested in being notified of changes to the Timekeeper object. </li></ul><ul><li>The Observable object is also responsible for notifying all the Observer s when the Timekeeper sends a change notification. </li></ul><ul><li>Note that polymorphism plays an important role in the Observer pattern. </li></ul>
  10. 11. APPLYING OBSERVER IN POS SYSTEM <ul><li>Refer POS.java. </li></ul>

×