1 / 14

DESIGN PATTENS - OBSERVER PATTERN

DESIGN PATTENS - OBSERVER PATTERN. By, Srinivas Reddy.S. www.JAVA9S.com. A Simple Requirement. Display Boards. Websites. Mobile apps. www.JAVA9S.com. A Simple Requirement. Stock Market An application in the server monitors and gathers information related to Stock prices.

swann
Télécharger la présentation

DESIGN PATTENS - OBSERVER PATTERN

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DESIGN PATTENS- OBSERVER PATTERN By, SrinivasReddy.S www.JAVA9S.com

  2. A Simple Requirement Display Boards Websites Mobile apps www.JAVA9S.com

  3. A Simple Requirement.. • Stock Market • An application in the server monitors and gathers information related to Stock prices. • The changed stock prices should be informed to different parties like stock market display boards, mobile apps, websites etc., www.JAVA9S.com

  4. observable observers www.JAVA9S.com

  5. stockPricesChanged() stockPricesChanged(){ mobileDisplay.update(stockUpdates); stockBoardDisplay.update(stockUpdates); webdisplay.update(stockUpdates); } What if there are few more observers to be added? Violates the OO principles – Less dependency Less number of changes www.JAVA9S.com

  6. StockBoard Mobile Stock Update WEBSITE OTHER One to Many Relationship www.JAVA9S.com

  7. OBSERVER PATTERN • OBSERVER PATTERN defines one to many relationship between the objects. • When the state of the Observable object changes, all the observers will notified and updated automatically. www.JAVA9S.com

  8. Design – Observer pattern Collection of Observers Instance variable www.JAVA9S.com

  9. Design – Observer pattern - example www.JAVA9S.com

  10. Pull and Push model Stock Update Push model Update(Observable o, Map stocks) StockBoard Pull model update() *The observer should have a reference to Observable www.JAVA9S.com

  11. Code – Example www.JAVA9S.com

  12. Java's build in Observer pattern • java.util.Observable • java.util.Observer A concrete class An Interface • Issues: • Observable is a concrete class and due to • which the class which has the state will not have a chance • to have inheritance of its own. • Well know usage: • Java Swing • Java RMI www.JAVA9S.com

  13. Object Oriented Principles applied • Loose coupling between the Observer and Observable. • Programming to Interfaces. • Examples of Observer Pattern • Swing event listeners • Java RMI www.JAVA9S.com

  14. Thank you Thank you www.JAVA9S.com @java9s Pages - Java9s.com JAVA9S www.JAVA9S.com

More Related