Week 6, Class 2: Observer Pattern

Slides:



Advertisements
Similar presentations
The Observer Pattern SE-2811 Dr. Mark L. Hornick 1.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
3/15/05H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Design Patterns.
Week 5, Day 3: Observer Today Reducing coupling with the Observer The Observer pattern in Java APIs Posting events to a UI worker thread SE-2811 Slide.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Detailed design – class design Domain Modeling SE-2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
©Ian Sommerville 2004Software Engineering, 7th edition. Chapter 18 Slide 1 Software Reuse.
CS 210 Introduction to Design Patterns September 7 th, 2006.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
Behavioral Pattern: Observer C h a p t e r 5 – P a g e 186 A large monolithic design does not scale well as additional graphical and monitoring requirements.
Week 2, Day 2: The Factory Method Pattern Other good design principles Cohesion vs. Coupling Implementing the Strategy Pattern Changing strategies (behaviors)
Week 6, Class 1 & 2: Decorators Return Exam Questions about lab due tomorrow in class? Threads Locking on null object invokeLater & the squares example.
SE-2811 Software Component Design Week 1, Day 2 (and 1-3 and 2-1) SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick 1.
CS2852 Week 3, Class 2 Today Stacks Queues SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Week 7, Day 3 Half-Exam 2 A New Pattern SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder 1.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
Week 7, Class 1: The Command Pattern (cont.) Get Ready for Poll Everywhere Labs 2 & 3 returned Lab 7 due this evening at 11pm Quiz tomorrow at start of.
Slide design: Dr. Mark L. Hornick
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
SE-2811 Software Component Design Week 1, Day 1 Design pattern defined Code that needs a design pattern… SE-2811 Dr. Josiah Yoder Slide style: Dr. Hornick.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Week 6, Class 3: Composite Swing composites File composites Computer composites SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors:
Week 4, Day 1: Singleton(s?) Singleton leftover Why singletons? How to make lazy initialization work multi- threaded? Observers Why observers? Class structure.
Week 5, Class 3: Decorators Lab questions? Example: Starbuzz coffee Basic Pattern More examples Design Principles Compare with alternatives SE-2811 Slide.
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Software Architecture and Design BITS C461/IS C341 Software Engineering First Semester Aditya P. Mathur Department of Computer Science Purdue.
Software Design Refinement Using Design Patterns
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Slide design: Dr. Mark L. Hornick
Week 2, Day 1: The Factory Method Pattern
Observer Design Pattern
Observer Design Pattern
Architectural Patterns for Interactive Software
SE-2811 Software Component Design
SE /11/2018 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations.
Model-View-Controller
Design Patterns - A few examples
Week 6, Class 2: Observer Pattern
SE-2811 Software Component Design
SE-2811 Software Component Design
SE-1021 Software Engineering II
Introduction to Behavioral Patterns (1)
Advanced Program Design with C++
Week 7, Class 1: The Command Pattern (cont.)
Starting Design: Logical Architecture and UML Package Diagrams
Review: Design Pattern Structure
SE-2811 Software Component Design
Objects First with Java A Practical Introduction using BlueJ
Advanced ProgramMING Practices
8. Observer Pattern SE2811 Software Component Design
SE-2811 Software Component Design
Objects First with Java A Practical Introduction using BlueJ
12. Command Pattern SE2811 Software Component Design
Advanced ProgramMING Practices
11. MVC SE2811 Software Component Design
Week 8, Class 3: Model-View-Controller
SE-1021 Software Engineering II
11. MVC SE2811 Software Component Design
SE-1011 Slide design: Dr. Mark L. Hornick Instructor: Dr. Yoder
Software Design Lecture : 40.
Presentation transcript:

Week 6, Class 2: Observer Pattern 4/18/2019 Week 6, Class 2: Observer Pattern Today The Observer pattern Half-Exam 2: Week 7 Friday 17q2 1-3,6-7,9-12 SE-2811 Slide design: Dr. Mark L. Hornick Content: Dr. Hornick Errors: Dr. Yoder Dr. Yoder

SE-2811 4/18/2019 If you think the internet is not working in its current incarnation, you can’t change the system through think-pieces and F.C.C. regulations alone. You need new code [... you need cryptocurrency!]. Stephen Johnson, "Beyond the Bitcoin Bubble,” New York Times, Jan 15th 2018 [Interpretation added] 18q2-7-1 SE-2811 Dr. Mark L. Hornick Dr. Yoder

Motivating application: Microsoft Word How to update toolbars every time user clicks somewhere different in the document? [Demo in the real MSWord] SE-2811 Dr. Mark L. Hornick

Solution 1: (What kind of cohesion is this?) SE-2811 4/18/2019 Solution 1: (What kind of cohesion is this?) public void onClick(ClickEvent e) { if(cursorInBoldText()) { boldButton.setHighlight(); } styleDialog.setStyle(getCurrentCursorStyle()); if(selection.isActive()) { copyButton.setActive(); } /*… etc. … */ SE-2811 Dr. Yoder Dr. Yoder

Observer Pattern Context A system contains objects exhibiting: One-to-many dependency between objects One object changes state All dependents are notified and updated automatically SE-2811 Slide content originally by Dr. Hornick

What are we trying to achieve with the Observer Pattern ? Separation of software subsystems Separation between GUI & Domain objects Loosely-coupled classes to … Avoid editing code in multiple places Increase reusability Increase understanding Avoid polling A generic/elegant way for the classes to communicate SE-2811 Slide content originally by Dr. Hornick

Key components in the Observer Pattern Subject Subject has dependent observers. Observer(s) When the state of the subject changes, each dependent observer is notified. SE-2811 Slide content originally by Dr. Hornick

Slide content originally by Dr. Hornick Generic Subject class class SubjectClass implements Subject { public SubjectClass(); public void attach(Observer obs); public void detach(Observer obs); public void notifyObservers(); private ArrayList <Observer> observers; } Subject::attach(Observer* pObserver) { m_hObservers.push_back(pObserver); } Subject::detach(Observer* pObserver) m_hObservers.remove(pObserver); Subject::notify() Vector<Observer*>::iterator m_ppObserver; for (m_ppObserver = m_hObservers.begin();m_ppObserver = m_hObservers.end(); ++m_ppObserver) (*m_ppObserver)->update(); Note: Some texts define a notify() instead of notifyObservers() method. However, Java’s Object class already has a notify() method, which we don’t want to override. SE-2811 Slide content originally by Dr. Hornick

Slide content originally by Dr. Hornick Generic Observer class ObserverClass implements Observer { public ObserverClass(); public void update(???); } What is the appropriate argument for the update() method? SE-2811 Slide content originally by Dr. Hornick

Basic class relationships SE-2811 4/18/2019 Basic class relationships 18q2-7-1: Updated to resolve several issues SE-2811 Slide content originally by Dr. Hornick Dr. Yoder

Collaborations between objects in the Observer pattern s:SubjectClass o1:ObserverClass1 o2:ObserverClass2 attach() attach() notifyObservers() update(???) getContextSpecificInfo() update(???) getContextSpecificInfo() SE-2811 Slide content originally by Dr. Hornick

Implementation Questions What should the arguments of the update method be? Should we send the Subject as the argument? Should each instance of the Observer store the “concrete subject” as a data attribute, or just an Interface reference, or something else? Can Subject be an interface instead of an abstract class? SE-2811 Slide content originally by Dr. Hornick

Polling without Observer pattern 4/18/2019 Polling without Observer pattern s:SubjectClass o1:ObserverClass1 o2:ObserverClass2 changeState() getContextSpecificInfo() Not to be confused with "pole" or "polling" getContextSpecificInfo() getContextSpecificInfo() Dr. Yoder

Push in Observer Pattern 4/18/2019 Push in Observer Pattern s:SubjectClass o1:ObserverClass1 o2:ObserverClass2 attach() attach() notifyObservers() update(State coolStuff) update(State coolStuff) Dr. Yoder

Pull in Observer Pattern 4/18/2019 Pull in Observer Pattern s:SubjectClass o1:ObserverClass1 o2:ObserverClass2 attach() attach() notifyObservers() update() Not to be confused with "pole" or "polling" getContextSpecificInfo() update() getContextSpecificInfo() Dr. Yoder

Comparing Push and Pull SE-2811 4/18/2019 Comparing Push and Pull In teams of 2 or 3 Write two lists, one containing the advantages of push, the other, of pull. Write as many advantages as you can in 2 minutes. SE-2811 Dr. Mark L. Hornick Dr. Yoder

Java’s Observer Patterns 4/18/2019 Java’s Observer Patterns Java has multiple implementations of the Observable/Observer pattern java.util: Observable Observer javafx: javafx.scene.control.ButtonBase javafx.event.EventHandler SE-2811 Dr. Mark L. Hornick Dr. Yoder

java.util.Observable This “Subject” is a class Advantages: 4/18/2019 java.util.Observable This “Subject” is a class Advantages: Generic implementation (can’t implement in interface) Disadvantages: If you want to inherit from a different class… you can’t Must cast parameter to specific type Don’t get to code from scratch for the learning experience  SE-2811 Dr. Mark L. Hornick Dr. Yoder

LinearSubject example [see code example] SE-2811 Dr. Mark L. Hornick

UML diagram for the LinearSubject Observer 4/18/2019 UML diagram for the LinearSubject Observer [Circa SE-2811 Dr. Mark L. Hornick Dr. Yoder

Implementation Questions SE-2811 4/18/2019 Implementation Questions What should the arguments of the update method be? Should we send the Subject as the argument? Should each instance of the Observer store the “concrete subject” as a data attribute, or just an Interface reference? Can Subject be an abstract class instead of an Interface? SE-2811 Slide content originally by Dr. Hornick Dr. Yoder

Consequences (positive) 4/18/2019 Consequences (positive) Coupling between Subject and Observers: Subject knows it has a list of Observers, but not specific classes Each Observer conforms to the simple interface of the abstract Observer Interface. Hence, coupling is Minimal Abstract SE-2811 Slide content originally by Dr. Hornick Dr. Yoder

Consequences (positive) 4/18/2019 Consequences (positive) Cohesion is increased from single-class implementation State management and display/response are separated E.g. GUI innards separated from “your code” E.g. Web access separated from display SE-2811 Slide content originally by Dr. Hornick Dr. Yoder

Consequences (negative) 4/18/2019 Consequences (negative) Broadcast communication Notification is broadcast to all interested objects. Observers can be added/removed at any time. Observer decides when it needs to be notified. Unexpected updates Observers have no knowledge Of each other’s presence. About the cost of “state change of subject” Cascade of updates. SE-2811 Slide content originally by Dr. Hornick Dr. Yoder

SE-2811 4/18/2019 https://www.polleverywhere.com/free_text_polls/zIp2tH2IVWjXv4H SE-2811 Dr. Mark L. Hornick Dr. Yoder