Component-Based Software Engineering Using Interfaces Paul Krause.

31
Component-Based Component-Based Software Engineering Software Engineering Using Interfaces Using Interfaces Paul Krause Paul Krause

Transcript of Component-Based Software Engineering Using Interfaces Paul Krause.

Page 1: Component-Based Software Engineering Using Interfaces Paul Krause.

Component-BasedComponent-BasedSoftware EngineeringSoftware Engineering

Using InterfacesUsing Interfaces

Paul KrausePaul Krause

Page 2: Component-Based Software Engineering Using Interfaces Paul Krause.

Lecture 4 - Lecture 4 - Using InterfacesUsing Interfaces

ContentsContents IntroductionIntroduction Interfaces vs. Abstract ClassesInterfaces vs. Abstract Classes A Digression on “Casting”A Digression on “Casting”

making lawyers humanmaking lawyers human Back to the Phone DirectoryBack to the Phone Directory

Page 3: Component-Based Software Engineering Using Interfaces Paul Krause.

IntroductionIntroduction

We will use a Telephone Directory as a We will use a Telephone Directory as a case study to illustrate the use of case study to illustrate the use of components and interfacescomponents and interfaces

This is taken from:This is taken from:Objects, Abstraction, Data Structures and Design Objects, Abstraction, Data Structures and Design

Using Java Version 5.0Using Java Version 5.0

Authors: Elliot Koffman and Paul WolfgangAuthors: Elliot Koffman and Paul Wolfgang

PublisherPublisher: : John WileyJohn Wiley

Page 4: Component-Based Software Engineering Using Interfaces Paul Krause.

InterfacesInterfaces

Sometimes we may find two or more Sometimes we may find two or more different subclasses share some common different subclasses share some common behaviourbehaviour

In this case, they are not strictly “kinds of” In this case, they are not strictly “kinds of” some common parentsome common parent

Rather, they “behave like” some common Rather, they “behave like” some common pattern (under certain circumstances)pattern (under certain circumstances)

We say that they both implement a We say that they both implement a common interfacecommon interface

Page 5: Component-Based Software Engineering Using Interfaces Paul Krause.

A “Cashier” InterfaceA “Cashier” Interface

Person Machine

ATMEmployee

«interface»Cashier

withdrawdeposit

Page 6: Component-Based Software Engineering Using Interfaces Paul Krause.

Java interfacesJava interfaces

public interface Cashier {public interface Cashier {

public void deposit(int id, double amount);public void deposit(int id, double amount);

public boolean withdraw(int id, double amount);public boolean withdraw(int id, double amount);

}}

Note:Note: An interface is pure specification - it contains An interface is pure specification - it contains

no implementationno implementation

Page 7: Component-Based Software Engineering Using Interfaces Paul Krause.

Lecture 4 - Lecture 4 - Using InterfacesUsing Interfaces

ContentsContents IntroductionIntroduction Interfaces vs. Abstract ClassesInterfaces vs. Abstract Classes A Digression on “Casting”A Digression on “Casting”

making lawyers humanmaking lawyers human Back to the Phone DirectoryBack to the Phone Directory

Page 8: Component-Based Software Engineering Using Interfaces Paul Krause.

cf Abstract Classescf Abstract Classes

Page 9: Component-Based Software Engineering Using Interfaces Paul Krause.

Abstract Class vs. InterfaceAbstract Class vs. Interface

public abstract class EllipticalShape {public abstract class EllipticalShape {public abstract double area( );public abstract double area( );public abstract double circumference( );public abstract double circumference( );

}}

public interface Cashier {public interface Cashier {public void deposit(int id, double amount);public void deposit(int id, double amount);public boolean withdraw(int id, double amount);public boolean withdraw(int id, double amount);

}}

Page 10: Component-Based Software Engineering Using Interfaces Paul Krause.

What are the differences?What are the differences?

Abstract ClassAbstract Class Can include class and Can include class and

instance fieldsinstance fields May include concrete May include concrete

implementations of implementations of methodsmethods

A concrete class can A concrete class can only extend a only extend a singlesingle abstract classabstract class

InterfaceInterface Can only include Can only include

static final fieldsstatic final fields All methods must be All methods must be

abstract declarations - abstract declarations - no implementationno implementation

A class can A class can implement implement multiplemultiple interfacesinterfaces

Page 11: Component-Based Software Engineering Using Interfaces Paul Krause.

Why use interfaces?Why use interfaces?

Design Guideline:Design Guideline:

““When the functionality supported by a class When the functionality supported by a class can be implemented in different ways, it is can be implemented in different ways, it is advisable to separate the interface from advisable to separate the interface from the implementation”the implementation”

Xiaoping JiaXiaoping Jia

Object-Oriented Software Development Object-Oriented Software Development Using JavaUsing Java

Page 12: Component-Based Software Engineering Using Interfaces Paul Krause.

Chili PizzaExpressChili PizzaExpressEventObject

source

getSource()toString()

Bakery

addOrderListener()removeOrderListener()sendMessage(PizzaEvent)

fires passed to

registers with 0..*

invokes notifications in 0..*

«interface»OrderListener

pizzaStatus(evt)

Customer

run( )

iNumberiSliceNumber

PizzaEvent

Page 13: Component-Based Software Engineering Using Interfaces Paul Krause.

Class BakeryClass Bakery

private void sendMessage(PizzaEvent anEvent) {private void sendMessage(PizzaEvent anEvent) {Vector v;Vector v;synchronised(this) {synchronised(this) { v = (Vector) iCustomers.clone( );v = (Vector) iCustomers.clone( ); }}for (int i=0; i<v.size( ); i++) {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); }}}}

}}

Page 14: Component-Based Software Engineering Using Interfaces Paul Krause.

General IdiomGeneral Idiom

To send an event to a Customer, the To send an event to a Customer, the Bakery needs to know:Bakery needs to know: the name of the method to invoke in a the name of the method to invoke in a

CustomerCustomer the type of the Event to send when invoking the type of the Event to send when invoking

the respective methodthe respective method this information is contained in the this information is contained in the

OrderListener interfaceOrderListener interface that is all the Bakery needs to knowthat is all the Bakery needs to know

Page 15: Component-Based Software Engineering Using Interfaces Paul Krause.

Lecture 4 - Lecture 4 - Using InterfacesUsing Interfaces

ContentsContents IntroductionIntroduction Interfaces vs. Abstract ClassesInterfaces vs. Abstract Classes A Digression on “Casting”A Digression on “Casting”

making lawyers humanmaking lawyers human Back to the Phone DirectoryBack to the Phone Directory

Page 16: Component-Based Software Engineering Using Interfaces Paul Krause.

A Diversion on “Casting”A Diversion on “Casting”

In the last example, we were only In the last example, we were only interested in the “OrderListener” behaviour interested in the “OrderListener” behaviour of Customers:of Customers:

OrderListener ol = (OrderListener)v.elementAt(i);OrderListener ol = (OrderListener)v.elementAt(i); We “cast” an element of the Vector vWe “cast” an element of the Vector v This is a way of converting one type to This is a way of converting one type to

another, subject to certain rulesanother, subject to certain rules

Page 17: Component-Based Software Engineering Using Interfaces Paul Krause.

Casting between Data TypesCasting between Data Types

Suppose I wish to add a double precision Suppose I wish to add a double precision value to an integer, and just keep the value to an integer, and just keep the integer value of the answer:integer value of the answer:double x = 24.2;double x = 24.2;

int y = 1;int y = 1;

int z = (int) (x - y);int z = (int) (x - y); We have explicitly cast (“converted”) the We have explicitly cast (“converted”) the

result of the calculation into an integerresult of the calculation into an integer

Page 18: Component-Based Software Engineering Using Interfaces Paul Krause.

Rules for Data TypesRules for Data Types

An explicit cast is needed when converting An explicit cast is needed when converting from a “larger” (more information) type to a from a “larger” (more information) type to a “smaller” one (e.g. “smaller” one (e.g. doubledouble to to intint))

This is not needed when casting This is not needed when casting toto a a larger type - the compiler will do it for yoularger type - the compiler will do it for you

Page 19: Component-Based Software Engineering Using Interfaces Paul Krause.

Rules for Casting ObjectsRules for Casting Objects

An instance of a class may only be cast into subclasses An instance of a class may only be cast into subclasses or superclasses in its inheritance hierarchyor superclasses in its inheritance hierarchyHuman h;Human h;

Lawyer l;Lawyer l;

l = new Lawyer( );l = new Lawyer( );

h = (Human) l;h = (Human) l;

You can use an instance of a class, anywhere an You can use an instance of a class, anywhere an instance of one of its superclasses is expectedinstance of one of its superclasses is expected

Page 20: Component-Based Software Engineering Using Interfaces Paul Krause.

W.r.t. InterfacesW.r.t. Interfaces

You can cast an object to an interface type You can cast an object to an interface type if (and if (and onlyonly if) the class of that object if) the class of that object implements the interfaceimplements the interface

This will give full access to the services This will give full access to the services provided by the interface,e.g.:provided by the interface,e.g.:

OrderListener ol = (OrderListener)v.elementAt(i);OrderListener ol = (OrderListener)v.elementAt(i);

Page 21: Component-Based Software Engineering Using Interfaces Paul Krause.

Lecture 4 - Lecture 4 - Using InterfacesUsing Interfaces

ContentsContents IntroductionIntroduction Interfaces vs. Abstract ClassesInterfaces vs. Abstract Classes A Digression on “Casting”A Digression on “Casting”

making lawyers humanmaking lawyers human Back to the Phone DirectoryBack to the Phone Directory

Page 22: Component-Based Software Engineering Using Interfaces Paul Krause.

Class Diagram Class Diagram (from Koffman and Wolfgang)(from Koffman and Wolfgang)

Page 23: Component-Based Software Engineering Using Interfaces Paul Krause.

Directory Entry ClassDirectory Entry Class

Page 24: Component-Based Software Engineering Using Interfaces Paul Krause.

PhoneDirectory InterfacePhoneDirectory Interface

Page 25: Component-Based Software Engineering Using Interfaces Paul Krause.

(At least) Two implementations (At least) Two implementations of PDUserInterfaceof PDUserInterface

<<Interface>>PDUserInterface

processCommands( )

PDConsoleUI

processCommands( )

PDGUI

processCommands( )

Page 26: Component-Based Software Engineering Using Interfaces Paul Krause.

An Implementation of the An Implementation of the PhoneDirectory Interface (I)PhoneDirectory Interface (I)

Page 27: Component-Based Software Engineering Using Interfaces Paul Krause.
Page 28: Component-Based Software Engineering Using Interfaces Paul Krause.

An Implementation of the An Implementation of the PhoneDirectory Interface (II)PhoneDirectory Interface (II)

Page 29: Component-Based Software Engineering Using Interfaces Paul Krause.

Java ImplementationJava ImplementationArrayBasedPD

+ 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

Page 30: Component-Based Software Engineering Using Interfaces Paul Krause.

Exercise: Implement this!Exercise: Implement this!

DirectoryEntry

+ DirectoryEntry(String name, String number )+ String getName( )+ String getNumber( )+ void setNumber(String number)

- String name- String number

Page 31: Component-Based Software Engineering Using Interfaces Paul Krause.

Phone Directory InterfacePhone Directory Interfacepublic interface PhoneDirectory {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. * @param sourceName The name of the file (data source)* @param 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 * @param name The name of the person to be looked up* @param name The name of the person to be looked up * @return The number, or null if the name is not in the directory* @return The number, or null if the name is not in the directory */*/ String lookupEntry(String name);String lookupEntry(String name);

……