Component-Based Software Engineering Using Interfaces Paul Krause.

Slides:



Advertisements
Similar presentations
Cmp Sci 187: Introduction to Software Design Following Chapter 1 of text (Koffmann and Wolfgang)
Advertisements

1 The Software Challenge People may come and go, but software may remain A software product is often expected to be used for an extended period of time.
Software Design. The Software Challenge In industry, a software product is expected to be used for an extended period of time by someone who did not write.
Inheritance Inheritance Reserved word protected Reserved word super
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
ITEC200 – Week03 Inheritance and Class Hierarchies.
ITEC200 Week01 Introduction to Software Design.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented Software Engineering More Modelling with Classes Paul J Krause.
Fall 2007CS 225 Introduction to Software Design Chapter 1.
Object-Oriented Programming with Java The Java Event Model Lecture 5.
Object-Oriented Programming with Java Lecture 6 Using the Java Event Model.
Object-Oriented Programming with Java Exceptions, Strings and Things Lecture 4.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives To become familiar with the software challenge.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CPSC150 Abstract Classes and Interfaces Chapter 10.
Object-Oriented Programming with Java Lecture 2 The Java Event Model.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Internet Software Development The Java Event Model Lecture 5.
Component-Based Software Engineering Components and Interfaces Paul Krause.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Practical Object-Oriented Design with UML 2e Slide 1/1 ©The McGraw-Hill Companies, 2004 PRACTICAL OBJECT-ORIENTED DESIGN WITH UML 2e Chapter 2: Modelling.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Internet Software Development Classes and Inheritance Paul J Krause.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Case Study: Designing A Telephone Directory Program Ellen Walker CPSC 201 Data Structures Hiram College Includes figures from Objects, Abstraction & Data.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Introduction to Software Design Chapter 1. Chapter Objectives  To become familiar with the software challenge and the software life cycle  To understand.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Copyright © 2002, Systems and Computer Engineering, Carleton University EventModel.ppt * Object-Oriented Software Development Part 17.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Object-Oriented Programming: Polymorphism Chapter 10.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Sixth Lecture ArrayList Abstract Class and Interface
Object-Orientated Analysis, Design and Programming
Week 6 Object-Oriented Programming (2): Polymorphism
Component-Based Software Engineering
Component-Based Software Engineering
Chapter 8 Class Inheritance and Interfaces
Presentation transcript:

Component-Based Software Engineering Using Interfaces Paul Krause

Lecture 4 - Using Interfaces Contents  Introduction  Interfaces vs. Abstract Classes  A Digression on “Casting” making lawyers human making lawyers human  Back to the Phone Directory

Introduction  We will use a Telephone Directory as a case study to illustrate the use of components and interfaces  This is taken from: Objects, Abstraction, Data Structures and Design Using Java Version 5.0 Authors: Elliot Koffman and Paul Wolfgang Publisher : John Wiley

Interfaces  Sometimes we may find two or more different subclasses share some common behaviour  In this case, they are not strictly “kinds of” some common parent  Rather, they “behave like” some common pattern (under certain circumstances)  We say that they both implement a common interface

A “Cashier” Interface PersonMachine ATMEmployee «interface» Cashier withdraw deposit

Java interfaces public interface Cashier { public void deposit(int id, double amount); public boolean withdraw(int id, double amount); }  Note: An interface is pure specification - it contains no implementation An interface is pure specification - it contains no implementation

Lecture 4 - Using Interfaces Contents  Introduction  Interfaces vs. Abstract Classes  A Digression on “Casting” making lawyers human making lawyers human  Back to the Phone Directory

cf Abstract Classes

Abstract Class vs. Interface public abstract class EllipticalShape { public abstract double area( ); public abstract double circumference( ); } public interface Cashier { public void deposit(int id, double amount); public boolean withdraw(int id, double amount); }

What are the differences? Abstract Class  Can include class and instance fields  May include concrete implementations of methods  A concrete class can only extend a single abstract class Interface  Can only include static final fields  All methods must be abstract declarations - no implementation  A class can implement multiple interfaces

Why use interfaces?  Design Guideline: “When the functionality supported by a class can be implemented in different ways, it is advisable to separate the interface from the implementation” Xiaoping Jia Object-Oriented Software Development Using Java

Chili PizzaExpress EventObject source getSource() toString() Bakery addOrderListener() removeOrderListener() sendMessage(PizzaEvent) fires passed to registers with 0..* invokes notifications in0..* «interface» OrderListener pizzaStatus(evt) Customer run( ) iNumber iSliceNumber PizzaEvent

Class Bakery private void sendMessage(PizzaEvent anEvent) { Vector v; synchronised(this) { v = (Vector) iCustomers.clone( ); v = (Vector) iCustomers.clone( ); } for (int i=0; i<v.size( ); i++) { OrderListener ol = (OrderListener)v.elementAt(i); OrderListener ol = (OrderListener)v.elementAt(i); ol.pizzaStatus(anEvent); ol.pizzaStatus(anEvent); }}}

General Idiom  To send an event to a Customer, the Bakery needs to know: the name of the method to invoke in a Customer the name of the method to invoke in a Customer the type of the Event to send when invoking the respective method the type of the Event to send when invoking the respective method  this information is contained in the OrderListener interface that is all the Bakery needs to know that is all the Bakery needs to know

Lecture 4 - Using Interfaces Contents  Introduction  Interfaces vs. Abstract Classes  A Digression on “Casting” making lawyers human making lawyers human  Back to the Phone Directory

A Diversion on “Casting”  In the last example, we were only interested in the “OrderListener” behaviour of Customers: OrderListener ol = (OrderListener)v.elementAt(i);  We “cast” an element of the Vector v  This is a way of converting one type to another, subject to certain rules

Casting between Data Types  Suppose I wish to add a double precision value to an integer, and just keep the integer value of the answer: double x = 24.2; int y = 1; int z = (int) (x - y);  We have explicitly cast (“converted”) the result of the calculation into an integer

Rules for Data Types  An explicit cast is needed when converting from a “larger” (more information) type to a “smaller” one (e.g. double to int)  This is not needed when casting to a larger type - the compiler will do it for you

Rules for Casting Objects  An instance of a class may only be cast into subclasses or superclasses in its inheritance hierarchy Human h; Lawyer l; l = new Lawyer( ); h = (Human) l;  You can use an instance of a class, anywhere an instance of one of its superclasses is expected

W.r.t. Interfaces  You can cast an object to an interface type if (and only if) the class of that object implements the interface  This will give full access to the services provided by the interface,e.g.: OrderListener ol = (OrderListener)v.elementAt(i);

Lecture 4 - Using Interfaces Contents  Introduction  Interfaces vs. Abstract Classes  A Digression on “Casting” making lawyers human making lawyers human  Back to the Phone Directory

Class Diagram (from Koffman and Wolfgang)

Directory Entry Class

PhoneDirectory Interface

(At least) Two implementations of PDUserInterface > PDUserInterface processCommands( ) PDConsoleUI processCommands( ) PDGUI processCommands( )

An Implementation of the PhoneDirectory Interface (I)

An Implementation of the PhoneDirectory Interface (II)

Java Implementation ArrayBasedPD + loadData( ) + addOrChangeEntry( ) +lookupEntry( ) + removeEntry( ) + save( ) DirectoryEntry + DirectoryEntry(String name, String number ) + String getName( ) + String getNumber( ) + void setNumber(String number) - String name - String number collects together is stored in 0.. * 1

Exercise: Implement this! DirectoryEntry + DirectoryEntry(String name, String number ) + String getName( ) + String getNumber( ) + void setNumber(String number) - String name - String number

Phone Directory Interface public interface PhoneDirectory { /** Load the data file containing the directory, or /** Load the data file containing the directory, or * establish a connection with the data source. * establish a connection with the data source. sourceName The name of the file (data source) sourceName The name of the file (data source) * with the phone directory entries * with the phone directory entries */ */ void loadData(String sourceName); void loadData(String sourceName); /** Look up an entry /** Look up an entry name The name of the person to be looked up name The name of the person to be looked up The number, or null if the name is not in the directory The number, or null if the name is not in the directory */ */ String lookupEntry(String name); String lookupEntry(String name);…