Component-Based Software Engineering Using Interfaces Paul Krause.
-
Upload
gabriella-webb -
Category
Documents
-
view
219 -
download
0
Transcript of Component-Based Software Engineering Using Interfaces Paul Krause.
Component-BasedComponent-BasedSoftware EngineeringSoftware Engineering
Using InterfacesUsing Interfaces
Paul KrausePaul 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
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
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
A “Cashier” InterfaceA “Cashier” Interface
Person Machine
ATMEmployee
«interface»Cashier
withdrawdeposit
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
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
cf Abstract Classescf Abstract Classes
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);
}}
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
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
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
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); }}}}
}}
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
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
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
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
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
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
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);
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
Class Diagram Class Diagram (from Koffman and Wolfgang)(from Koffman and Wolfgang)
Directory Entry ClassDirectory Entry Class
PhoneDirectory InterfacePhoneDirectory Interface
(At least) Two implementations (At least) Two implementations of PDUserInterfaceof PDUserInterface
<<Interface>>PDUserInterface
processCommands( )
PDConsoleUI
processCommands( )
PDGUI
processCommands( )
An Implementation of the An Implementation of the PhoneDirectory Interface (I)PhoneDirectory Interface (I)
An Implementation of the An Implementation of the PhoneDirectory Interface (II)PhoneDirectory Interface (II)
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
Exercise: Implement this!Exercise: Implement this!
DirectoryEntry
+ DirectoryEntry(String name, String number )+ String getName( )+ String getNumber( )+ void setNumber(String number)
- String name- String number
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);
……