Designing with Interfaces

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CHAPTER 3 COLLECTIONS Abstract Data Types. 2 A data type consists of a set of values or elements, called its domain, and a set of operators acting on.
Design Patterns. What is a Design Pattern? Generic pattern for solving a certain class of problem Learn to recognize the class of problem and apply a.
The Observer Pattern. Formal Definition Define a one-to-many dependency between objects so that when one object changes state, all its dependents are.
Object-Oriented Analysis and Design
1 Object Oriented Design & Patterns Part 1. 2 Design Patterns Derived from architectural patterns: –rules for design of buildings –describe common problems,
More OOP Design Patterns
DESIGN PATTENS - OBSERVER PATTERN
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
Computer Science 209 The Strategy Pattern I: Comparisons and Layouts.
CSC 313 – Advanced Programming Topics. Observer Pattern Intent  Efficiently perform 1-to-many communication  Easy to respond dynamically when event(s)
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.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Architectural pattern: Interceptor Source: POSA II pp 109 – 140POSA II Environment: developing frameworks that can be extended transparently Recurring.
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.
CSC 480 Software Engineering Design With Patterns.
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.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
L10: Model-View-Controller General application structure. User Interface: Role, Requirements, Problems Design patterns: Model – View – Controller, Observer/Observable.
Observer Pattern Keeping An Eye on Things Need to introduce observer pattern formally first, include book definition & design principle Keeping An Eye.
OBSERVER PATTERN OBSERVER PATTERN Presented By Presented By Ajeet Tripathi ISE
3/1/01H-1 © 2001 T. Horton CS 494 Object-Oriented Analysis & Design Evaluating Class Diagrams Topics include: Cohesion, Coupling Law of Demeter (handout)
Chapter 5 Patterns and GUI Programming -Part 2-. COMPOSITE Pattern Containers and Components Containers collect GUI components Sometimes, want to add.
Observer Pattern Context:
Iterators.
Java SWING and Model View Controller (MVC)
EECE 310: Software Engineering
MPCS – Advanced java Programming
Chapter 19 Java Data Structures
ADT’s, Collections/Generics and Iterators
Observer Design Pattern
Interfaces James Brucker.
Observer Design Pattern
Architectural Patterns for Interactive Software
Interfaces and Inheritance
Intent (Thanks to Jim Fawcett for the slides)
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Java Collections Overview
Linked Lists.
08/15/09 Design Patterns James Brucker.
(Java Collections Framework) AbstractSequentialList
Introduction to Behavioral Patterns (1)
Model-View-Controller (MVC) Pattern
Patterns.
CSC 480 Software Engineering
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Behavioral Patterns Part-I introduction UNIT-VI
08/15/09 Decorator Pattern Context: We want to enhance the behavior of a class, and there may be many (open-ended) ways of enhancing the class. The enhanced.
GoF Design Patterns (Ch. 26)
Design pattern Lecture 9.
Interator and Iterable
Professor John Canny Spring 2004 March 5
A few of the more significant changes and additions. James Brucker
Introduction to Inheritance
Lexical Ordering and Sorting
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Design Patterns Lecture part 1.
Abstract Method & Abstract Classes
CSC 480 Software Engineering
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
11. MVC SE2811 Software Component Design
11. MVC SE2811 Software Component Design
Chapter 4: Threads.
Introduction to Java Collection
Presentation transcript:

Designing with Interfaces

"Program to an interface, not an implementation" OO A&D Principle "Program to an interface, not an implementation" meaning: "Program to the specification (of an object's behavior), don't depend on its implementation (which may change)".

Designing with Interfaces (1) Use interfaces to "protect" one class from another class whose implementation may change. Reduces coupling between classes. Define what behavior a class must provide. BankAccount - balance: Money + deposit(Money) + withdraw(Money) Money - currency = "Baht" - value: BigDecimal + getValue( ) + getCurrency( ) + add( m: Money ) contains

Designing with Interfaces (2) Create an interface for the required behavior. Clients use the Interface type, not the actual type. Providers implement the interface. BankAccount - balance: Money <<interface>> Money + getValue( ) + getCurrency( ) + add( Money ) ThaiMoney

Designing with Interfaces Interface can be used to define required behavior of a client. Arrays.sort( students ) // array of Student objects Application Utility Class <<interface>> Comparable Arrays ... s1.compareTo(s2) +compareTo() +sort(Comparable [ ] x) Arrays.sort( ) can sort any array of objects that implements Comparable Student ... +compareTo()

<<interface>> Iterator Interface Pattern: we want to visit every member of a collection, and we want this to work for any kind of collection. Solution: design an interface for the behavior we want. Require that all collections implement this interface. MyApplication <<interface>> Iterator +hasNext( ): bool +next( ): Object +process(Iterator) ArrayList

Interface for the Observer Pattern Pattern: one object is the source if "interesting" events. Other objects want to be notified when an interesting event occurs. Solution: objects register themselves as Observers. Then the "interesting" event occurs, the source calls the Observers' update( ) method. Observers Source addObserver MyObserver App update MyObserver[]: who; +update(Object src, Object info) +addObserver( obj ) +deleteObserver( obj )

Interface for Observer Pattern The Java Observer interface specifies client behavior Observable abstract class provides the server side. Client Interesting Source <<interface>> Observer Observable +update( ... ) +addObserver( Observer ) +deleteObserver( Observer ) +notifyObservers( ... ) +countObservers( ... ) AnyClient ... addObserver( this ) extends +update(...) MyApplication

Interface for View-Controller Pattern Interfaces are used to separate an application's "user interface" from the "logic engine" of the application. Interface reduces dependency between classes. User Interface Controller <<interface>> ClientUI App ClientUI: obj; +display() +getReply(question) +run( obj ) GUIclient App.run( this ) ... s = obj.getReply("Enter amount:") +display() +getReply(question)

Interfaces You Should Know Interface What it specifies Runnable run( ) method Comparable<T> compareTo( T other ) Comparator<T> compare(T x, T y) Iterator<T> hasNext() and next() iterating over collections Iterable<T> iterator() - create an Iterator a way of creating iterators Cloneable safe to call clone()