Recitation 11 November 11, 2011. Today’s Goals:  Automatic refresh  Learn and apply the Observer pattern.

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Mari Göransson - KaU - Datavetenskap - DAVD11 1 Java Beans - Events and Properties -
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Run-Time Storage Organization
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Topic 3 The Stack ADT.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Java Event Handling CSIS 3701: Advanced Object Oriented Programming.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
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.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Data structures Abstract data types Java classes for Data structures and ADTs.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
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.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
First Bean Compose SimpleBean Demo Simple Bean Discuss Manifest and Jar Add Properties to SimpleBean.
As you arrive… FIRST: Grab the handout SECOND: Snarf the code for today’s class THIRD: Explain what’s going on in this code (It’s Example 1 in the code.
CSCI 6962: Server-side Design and Programming JSF DataTables and Shopping Carts.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
Recitation 7 Collections. Array List and Linked List Array List and Linked List are implementations of the same interface: List. As a result, they have.
Programming Languages and Paradigms Activation Records in Java.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Events Programming in C# Events CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Java Beans - Basics CIS 421 Web-based Java Programming.
C OMP 401 P RACTICAL A PPLICATIONS OF MVC AND O BSERVER : P ROPERTY N OTIFICATION Instructor: Prasun Dewan.
1 Linked Lists Assignment What about assignment? –Suppose you have linked lists: List lst1, lst2; lst1.push_front( 35 ); lst1.push_front( 18 ); lst2.push_front(
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Object Oriented Programming in Java Habib Rostami Lecture 7.
Recitation 5 September 23, As you come in...  Please sit next to someone you will collaborate with for today’s recitation.  Choose someone with.
Stacks – Cont’d Nour El-Kadri ITI Summary We have seen that arrays are efficient data structures. Accessing any element of an array necessitates.
Java Visual Applications CSIS 3701: Advanced Object Oriented Programming.
Dr. Bernard Chen Ph.D. University of Central Arkansas
ARRAYLIST AND VECTOR.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Arrays and Collections
Object Oriented Programming in java
CSE 373 Data Structures and Algorithms
Recitation 7 October 7, 2011.
Recitation 10 November 3, 2011.
Stacks.
Recitation 9 October 28, 2011.
Properties and Collections
Creating and Modifying Text part 3
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Classes and Objects Object Creation
Presentation transcript:

Recitation 11 November 11, 2011

Today’s Goals:  Automatic refresh  Learn and apply the Observer pattern

Fixing the Refresh Problem  Tell Object Editor when changes have occurred  Observer pattern: “Observable” Object Obervers / Listeners Events / Notifications

Analogy: AlertCarolina  You register / listen for alerts  When the state changes, you receive a notification indicating:  State has changed  Information relevant to the change

Two Steps in Observer Pattern  1) Observers register with observable objects to listen for events (one time only)  2) When observable object undergoes an appropriate change, it sends an informative notification to all registered observers

Step 1: Registration (Properties)  Observable class must implement: public void addPropertyChangeListener( PropertyChangeListener listener)  This method adds the provided listener to a collection of listeners (as long as the collection doesn’t already contain this listener – avoid double notifications)  If ObjectEditor.edit() is called on a class that implements this method, ObjectEditor will automatically register itself for notifications from this class

Step 2: Notification (Properties)  When the observed object changes state, it calls propertyChange( PropertyChangeEvent event) on each of its observers  Construct a PropertyChangeEvent with:  this (i.e. a pointer to the observed object)  The name of the changed property as a String  “width”, “x”, “knight”, etc.  The old value of the property  The new value of the property

Step 2 Example: setWidth(int newWidth) int oldWidth = width; width = newWidth; PropertyChangeEvent event = new PropertyChangeEvent(this, “width”, oldWidth, newWidth); /* call propertyChange(event) on all observers */

Collection Observers  Very similar to property observers  Must implement public void addVectorListener(VectorListener listener);  When collection changes, call on all registered listeners: updateVector(VectorChangeEvent event)

Vector Change Event package util.models; public class VectorChangeEvent { // constants to be used for event type public static final int AddComponentEvent = 1, DeleteComponentEvent = 2, ChangeComponentEvent = 3, InsertComponentEvent = 4, CompletedComponentsEvent = 5, ClearEvent = 6, UndefEvent = 1000; // constructor, oldObject can be null when no value is replaced public VectorChangeEvent(Object theSource, int type, int posn, Object oldObject, Object newObject, int newSize) { … } … }

Vector Change Event  To create a VectorChangeEvent within an Observer, pass the following into the VectorChangeEvent constructor (in order):  this  The int corresponding to the change that occurred, e.g. VectorChangeEvent.AddComponentEvent  The index of the affected element  The Object that was previously at that location (null for push())  The Object that is now at that location (null for pop())  The new size of the collection

Recitation Specification  Download Recitation11.zip from the Recitations page  Implement ObservableAlignedShapeStack:  Create an ArrayList instance variable that stores all objects listening to / observing this stack (VectorChangeListener objects)  public void addVectorListener(VectorListener listener) /* adds the provided listener to the list of listeners, but only if the listener is not already in the list */  Hint: Use the add() and contains() methods from ArrayList  Create: void notifyAllListeners(VectorChangeEvent event)  For each listener in the ArrayList, call propertyChange on that listener with the provided event  Override push() and pop() so that they notify all listeners of the change made to the collection (but only IF a change was made!)  If done correctly, you won’t need to manually refresh