Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
Advertisements

Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Object-Oriented Analysis and Design
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
More OOP Design Patterns
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 PATTENS - OBSERVER PATTERN
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Design Patterns.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAM DESIGN WITH C++ Model View.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
CS 350 – Software Design The Observer Pattern – Chapter 18 Let’s expand the case study to include new features: Sending a welcome letter to new customers.
Design Patterns Model – View – Controller. Copyright © 2001 DeLorme 28 November 2001 History ► A framework pattern for reusable applications. ► Depends.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
GUIs for Video Games. The Plan ● Layout suggestions ● Observer/Observable ● Steps to writing Observer/Observables ● Sample Code ● Practice.
CS 61B Data Structures and Programming Methodology July 3, 2008 David Sun.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Object Oriented Programming.  Interface  Event Handling.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Model View Controller (MVC) Bigger than a Pattern: It’s an Architecture Rick Mercer with help from many of others 1.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Chapter 5 Introduction to Defining Classes
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Observer Pattern.
MVC COMP 401 Fall 2014 Lecture 19 10/30/2014. Classic MVC 2
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
The Observer Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
Decorator COMP 401, Spring 2014 Lecture 15 10/14/2014.
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Part 1: Composition, Aggregation, and Delegation Part 2: Iterator COMP 401 Fall 2014 Lecture 10 9/18/2014.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Observer Pattern Context:
Observer Design Pattern
Architectural Patterns for Interactive Software
Design Patterns Model – View – Controller
Event Driven Programming
Introduction to Behavioral Patterns (1)
Review: Design Pattern Structure
1/10/2019 JavaFX Events COSC 330.
Advanced ProgramMING Practices
Week 6, Class 2: Observer Pattern
Properties and Collections
Advanced ProgramMING Practices
Presentation transcript:

Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014

Observer / Observable Official Gang of Four description: – Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Observable – The object of “interest” Represents data or state that may change in the future. Observer – The “interested” objects Represents parts of system that need to be updated or may need to take action if/when observable object changes. 2

Use Cases User Interfaces – User interface elements like buttons, scrollbars, etc. are “observable” State changes correspond to clicks, drags, etc. – Application objects that must respond to user’s interactions with the UI are “observers” Asynchronous Programming – Also known as “event-based” programming – May have well-defined actions corresponding to events that may occur, but can’t know in advance which event will occur or when it will occur. Building block / component in other design patterns. 3

Basic Observer/Observable Defining Observer as an interface allows any class to act as an observer for the Observable class. Notice that Observable can have more than one observer. – And that they don’t know about each other or the order of update. 4 interface Observer { void update(); } class Observable { List observers; void addObserver(Observer o) { // Adds o to list of observers observers.add(o); } void deleteObserver(Observer o) { // Takes o off list of observers observers.remove(o); } void notifyObservers() { // Trigger update method on // all observers for (Observer o : observers) { o.update(); }

lec14.ex1 Game acts as Observable class GameObserver acts as Observer interface Discussion – How many games can one fan watch? – What if we wanted to have fan objects that could watch more than one game simultaneously?

Observer/Observable Refined Passing reference to observable as a parameter to update method allows Observer to register with more than one Observable and then detect which one changed. 6 interface Observer { void update(Observable o); } class Observable { List observers; void register(Observer o) { // Adds o to list of observers observers.add(o); } void unregister(Observer o) { // Takes o off list of observers observers.remove(o); } void notifyObservers() { // Trigger update method on // all observers for (Observer o : observers) { o.update(this); }

lec14.ex2 Same observers watching multiple games – Game object passed to fan as parameter to update method. – So far, fans are reacting to current state of game. But in real life, what do we react to? 7

Observer/Observable Refined More In addition to passing reference to Observable that changed, we can encapsulate what changed as additional information for the update method of the Observer. 8 interface Observer { void update(Observable o, Info i); } class Observable { List observers; void register(Observer o) { // Adds o to list of observers observers.add(o); } void unregister(Observer o) { // Takes o off list of observers observers.remove(o); } void notifyObservers(Info i) { // Trigger update method on // all observers for (Observer o : observers) { o.update(this, i); }

lec14.ex3 GameObserver update method now takes second argument – String who_scored This is the “additional information” about the change in the game that is being passed to the fans

Observer/Observable in java.util Java provides skeleton Observer / Observable that you can extend in java.util – Don’t have to use them. Our examples so far haven’t – The pattern is defined by relationship between objects and their interaction. Not the specific method names and/or implementation. Should be able to recognize Observer/Observable by these characteristics: – Observer object some how registers with observable. – Observable invokes a method on observers in order to signal state changes. 10

lec14.ex4 Game extends java.util.Observable – No longer have to manage our own list since we inherit that from Observable UNCFan and DukeFan implement java.util.Observer – Parameters to update are no longer context specific. update(Observable o, Object arg) – Need contravariant cast to make them useful Observable parent class requires state change to be marked by call to setChanged() before calling notifyObservers. – Otherwise, won’t do anything.

Delegating java.util.Observable Using Observable is convenient – Inherits basic Observable functionality without having to worry about doing it yourself. Must subclass observable. – Suppose your class already has a parent due to design or circumstance. Don’t have development control over class hierarchy. Parent class is part of third party library. Solution lies with delegation. 12

Delegating Observable Outline of solution: – Create a helper class that does subclass Observable Implements same interface as original class via delegation. Override notifyObservers to call setChanged() first. – Create an instance of this helper class as part of your object. – Provide methods from Observable via delegation to this instance. Delegation is being used both ways here. – Allows Observer to use “hidden” observable instance as if it were the original object. – Allows outside code to interact with original object as if it were Observable. lec14.ex5 13

Multiple Observer Lists Same delegation trick can be used to support multiple lists of observers. – Useful if you want to support more than one type of observable changes/events. – Requires providing a way to specify what is being observed. One approach: separate registration methods for each observable change/event. Another approach: additional argument to registration method. – May need to provide way of distinguishing between events as part of update info if you want the same observer to be able to observe more than one observable event. lec14.ex6 14