תוכנה 1 תבנית העיצוב Observer 1 שחר מעוז בית הספר למדעי המחשב אוניברסיטת תל אביב.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
IEG3080 Tutorial 7 Prepared by Ryan.
The Observer Pattern. Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Outline Introduction Problem Statement Object-Oriented Design Aspect-Oriented Design Conclusion Demo.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities Chapter 4.
Java Programming: Advanced Topics 1 Collections and Wealth of Utilities.
Observer Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
Design Patterns Lecture III. What Is A Pattern? Current use comes from the work of the architect Christopher Alexander Alexander studied ways to improve.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
CS 210 Introduction to Design Patterns September 7 th, 2006.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
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.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
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.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSC 313 – Advanced Programming Topics. Observer Pattern in Java  Java ♥ Observer Pattern & uses everywhere  Find pattern in JButton & ActionListener.
1 תרגול 11: Design Patterns ומחלקות פנימיות 1 תוכנה 1.
July 28, 2015IAT 2651 Design Patterns. “Gang of Four” July 28, 2015IAT 2652.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
OBSERVER DESIGN PATTERN. Behavioral Patterns  Behavioral patterns are those patterns that are most specifically concerned with communication between.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMMING PRACTICES Model View.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
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.
Computer Science 209 Software Development Inheritance and Composition.
List Interface and Linked List Mrs. Furman March 25, 2010.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
עקרונות תכנות מונחה עצמים תרגול 10: Generics. Outline  Generic classes  Generics & Inheritance  Wild Cards  Case study: Generic Graph.
1 תרגול 11: Design Patterns ומחלקות פנימיות אסף זריצקי ומתי שמרת 1 תוכנה 1.
IMPLEMENTATION OF CLASS EXTENT by Paweł Świetlicki.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Observer Pattern Context:
Slide design: Dr. Mark L. Hornick
Chapter 10 Design Patterns.
MPCS – Advanced java Programming
Design Patterns Part 1: Observer, Singleton, & Adapter
University of Central Florida COP 3330 Object Oriented Programming
Observer Design Pattern
Software Development Inheritance and Composition
Architectural Patterns for Interactive Software
Chapter 8, Design Patterns Bridge
Design Patterns - A few examples
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
Introduction to Behavioral Patterns (1)
ArrayLists.
Lecture 2: Implementing ArrayIntList reading:
Collections Framework
Advanced ProgramMING Practices
Design Patterns Lecture part 1.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Advanced ProgramMING Practices
Software Design Lecture : 40.
ArrayLists Readings: 10.1 (pg. 559 – 571).
Presentation transcript:

תוכנה 1 תבנית העיצוב Observer 1 שחר מעוז בית הספר למדעי המחשב אוניברסיטת תל אביב

תבניות עיצוב (Design Patterns) פתרונות כלליים לבעיות עיצוב שחוזרות על עצמן מגדירים שפה כללית יותר לדיון על עיצוב התכנית Factory, Singleton, Observer במקום "המחלקה A יורשת מהמחלקה B" ספר:Design Patterns: Elements of Reusable Object-Oriented Software מידע רב בנושא קיים ברשת בקורס ראינו כבר מספר תבניות, למשל Factory, Bridge, Adapter, Template method 2

Different Views Data a=50 b=10 C=20 3

Different Views (cont.) When the data changes, all views should change Views depend on data Views may vary, more added in the future Data store implementation may change We want: Separate the data aspect from the view one Notify views upon change in data 4

תבנית העיצוב Observer ניתן לתכנן את המחלקות גם אחרת! 5

Observer בג'אווה ג'אווה מספקת לנו מנשק Observer, ומחלקה ObservableObserverObservable נממש את Observer כדי ליצור subject, נכתוב מחלקה שיורשת מ-Observable. כבר נתונים לנו הוספה והסרה של Observers מסירת ההודעה ל- Observers הרשומים 6

Observer 7

Observable 8

9 Observable and Observer Observable addObserver change O1O1 O2O2 notify O3O3 O1O1 Observer O2O2 O3O3 9

Example Code - Subject public class IntegerDataBag extends Observable implements Iterable { private ArrayList list = new ArrayList (); public void add( Integer i ) { list.add(i); setChanged(); notifyObservers(); } public Iterator iterator() { return list.iterator(); } public Integer remove( int index ) { if( index < list.size() ) { Integer i = list.remove( index ); setChanged(); notifyObservers(); return i; } return null; } 10

Example Code - Observer public class IntegerAdder implements Observer { private IntegerDataBag bag; public IntegerAdder( IntegerDataBag bag ) { this.bag = bag; bag.addObserver( this ); } public void update(Observable o, Object arg) { if (o == bag) { println("The contents of the IntegerDataBag have changed."); int sum = 0; for (Integer i : bag) { sum += i; } println("The new sum of the integers is: " + sum); }... } 11