Title: Computer Science 209
1Computer Science 209
- The Criteria of a Good Interface
2What Is an Interface?
- The set of public methods of a class
- Well, not exactly, because a class can implement
several interfaces - Better A set of methods that supports a single
concept or purpose
3The Importance of Interfaces
- Interfaces are the glue that holds a software
system together. They allow software components
to - be developed independently
- be reused in other applications
- have several different implementations
4The Importance of Interfaces
- Interfaces separate the concerns of users and
implementers - If users and implementers obey this contract, the
rest is easy - We should strive to program to interfaces, not
implementations
5Criteria for a Good Interface
- Cohesion
- Completeness
- Convenience
- Carity
- Consistency
6Cohesion
- A class is an abstraction of a single concept and
should fulfill a coherent purpose - Methods in the interface should all deal with
that purpose - Irrelevant methods split into another class
7Completeness
- Provide all of the methods required to serve the
classs purpose - Dont leave out essential methods
8Convenience
- Make common tasks easy to perform
- Dont force users to jump through hoops to
perform simple tasks - Examples methods to examine the number of books
checked out by a patron and the current status of
a book
9Clarity
- Methods should have behavior and results that are
simple to describe and understand - Complex behavior should be split into two or more
methods
10Consistency
- Use conventional method names, parameters, return
types, and behavior wherever possible - Examples equals, toString, and compareTo
11Operations for Patron
Operations Create a Patron Examine the
name Examine the password Examine the
number of books checked out Increment the
number of books checked out Decrement the
number of books checked out Compare for
equality Return a string representation
Coherent? Complete? Convenient? Clear?
12Methods for Patron
Methods public Patron(String, String)
public String getName() public String
getPassword() public int getBooksOut()
public void incBooksOut() public void
decBooksOut() public boolean equals(Object
other) public String toString()
Consistent?
13Operations for Book
Operations Create a Book Examine the title
Examine the author Examine the current
borrower Examine the current status (checked
out or not) Borrow for a given patron
Return for a given patron Compare for
equality Return a string representation
Coherent? Complete? Convenient? Clear?
14Methods for Book
Methods public Book(String, String) public
String getTitle() public String getAuthor()
public boolean checkedOut() public Patron
getBorrower() public String borrowBook(Patron
p) public String returnBook(Patron p)
public String toString()
Consistent?
15The Library
Library
Book
0..3
0..1
Patron
PatronQueue
16Operations for Library
Operations Create a Library Add a patron
Remove a patron Add a book Remove a book
Borrow a book for a given patron Return a book
for a given patron Return a string
representation
Coherent? Complete? Convenient? Clear?
17Methods for Library
Methods public Library() public String
add(Patron p) public boolean remove(Patron p)
public String add(Book b) public boolean
remove(Book b) public String borrowBook(String
title, String patronName,
String password) public String
returnBook(String title, String patronName,
String password) public
String toString()
Consistent?