The Observer Pattern. Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are.

Slides:



Advertisements
Similar presentations
And so to Code. Forward, Reverse, and Round-Trip Engineering Forward Engineering Reverse Engineering Round-Trip Engineering.
Advertisements

Winter 2007ACS-3913 Ron McFadyen1 Also known as publish/subscribe The essence of this pattern is that one or more objects (called observers or listeners)
The Observer Pattern SE-2811 Dr. Mark L. Hornick 1.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Chapter 2: The Observer Pattern. Consider the Following Application Application specification Humidity Temperature Pressure Weather Station Weather Data.
MVC Nick Lopez Duplication of course material for any commercial purpose without the explicit written permission of the professor is prohibited.
Spring 2010ACS-3913 Ron McFadyen1 Weather Station Page 39+ In this application, weather station devices supply data to a weather data object. As the data.
JFC/Swing lectures O BSERVER PATTERN – general form Idea: decouple event from event handling Concrete Observable Abstract Observable Concrete Observer.
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
Oct Ron McFadyen1 Collaborations Collaboration : an arrangement of classes, links, roles in a context to implement some behaviour. Useful for.
Winter 2007ACS-3913 Ron McFadyen1 Observer Pattern Problem: There are many objects (observers / subscribers) needing to know of the state changes, or events,
Chair of Software Engineering OOSC - Summer Semester Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture.
1 Observer Design Pattern By Eric Perret Pages in Applying UML and Patterns.
DESIGN PATTENS - OBSERVER PATTERN
Multiple Choice Solutions True/False a c b e d   T F.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
1 Archface: Architectural Interface -- Bridging a Gap between Design Modeling and Implementation Naoyasu Ubayashi, Hidenori Akatoki, Jun Nomura Kyushu.
CSC 313 – Advanced Programming Topics. Lindsay Lohan Economy  Studies investigated economy of celebrities  Direct earnings from movies, music, TV, ads.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Go4 Visitor Pattern Presented By: Matt Wilson. Introduction 2  This presentation originated out of casual talk between former WMS “C++ Book Club” (defunct.
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.
Programming in C# Observer Design Pattern
GWT In-depth Explained by Rohit Ghatol
POSL (Principles of Software Languages) Gr. Kyushu Institute of Technology, Japan Pointcut-based Architectural Interface.
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.
Exercises on Basic OOP TCP1201: 2013/2014.  Problem: Design and build a computer hockey game.  Object: Hockey player  Attribute: Position, height,
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.
3-1 State Design Pattern C Sc 335 Rick Mercer. State Design Pattern State is one of the Behavioral patterns It is similar to Strategy Allows an object.
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.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Team 6 “The Unparseables” Design Patterns Chain of Responsibility Observer Flyweight 1.
Chapter 18 The Observer Pattern Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Observer Design Pattern
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
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.
CS 210 Review October 3, 2006.
Oct R McFadyen1 Observer Pattern Example From: Designed Patterns Explained by Shalloway & Trott; Addison-Wesley; P Observers: objects.
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.
Observer / Observable COMP 401 Fall 2014 Lecture 14 10/7/2014.
Interfaces. In order to work with a class, you need to understand the public methods  methods, return types,…  after you instantiate, what can you do.
Slide design: Dr. Mark L. Hornick
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
Interfaces & Abstract Classes (For CS III AP) March 2014.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
February 23, 2009Observer Pattern, OOA&D, Rubal Gupta, CSPP, Winter ‘09 Observer Pattern Defines a “one-to-many” dependency between objects so that when.
Construction Lecture Oo21 Gymnastics System Example Cont’d.
Observer Pattern Context:
Java SWING and Model View Controller (MVC)
Slide design: Dr. Mark L. Hornick
Observer Design Pattern
Architectural Patterns for Interactive Software
What is MVC Category: System MVC=Model-View-Controller
Designing with Interfaces
Introduction to Behavioral Patterns (1)
Introduction to Event Handling
Chapter 9 Behavioral Design Patterns
Observer Pattern 1.
Observer Pattern Example
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
8. Observer Pattern SE2811 Software Component Design
Week 6, Class 2: Observer Pattern
NETWORK PROGRAMMING CNET 441
Advanced ProgramMING Practices
GRASP.
Presentation transcript:

The Observer Pattern

Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

UML Diagram

Project Application This pattern is primarily used in our project to change the world when an event occurs. For example when a character moves it is the observer pattern that notifys all the hittable, and non-hittable objects in the world. This allows all screen objects to know the players position at all times and react accordingly.

Interfaces public interface Subject { public void addObserver( Observer o ); public void removeObserver( Observer o ); } public interface Observer { public void update( Subject o ); }

Code – Player Class public class Player implements Hittable, Runnable, Subject { private ArrayList observers = new ArrayList();.... public void moveRight(float amount) { x += amount; notifyObservers(); } public void moveLeft(float amount) { x -= amount; notifyObservers(); }

Code – Player Class public void addObserver( Observer o ) { observers.add( o ); } public void removeObserver( Observer o ) { observers.remove( o ); } private void notifyObservers() { // loop through and notify each observer Iterator i = observers.iterator(); while( i.hasNext() ) { Observer o = ( Observer ) i.next(); o.update( this ); }

Code – Hittable Class public class HSquare implements Hittable, Observer { private Player player;.... public HSquare(float x, float y, float w, float h) { player.addObserver(this); this.x = x; this.y = y; width = w; height = h; } public void update( Subject o ) { if( o == player ) { // Handling code goes here it is different for each object }

Changes needed to make this work? There really weren’t any changes besides adding the new methods.

Does this pattern help with code readability? This pattern helps manage which objects are connected to each other, however the code is no more readable than before.