API:Application Programmer - PowerPoint PPT Presentation

About This Presentation
Title:

API:Application Programmer

Description:

Used by a number of unrelated classes. Contains a known set of methods ... One of this method signature is: ... implements the interface based on its specific ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 10
Provided by: lahouar
Category:

less

Transcript and Presenter's Notes

Title: API:Application Programmer


1
Role of Interfaces in Large-Scale Software
  • APIApplication Programmers Interface The
    methods that allow a programmer to manipulate the
    data elements of a class
  • Sort A Real-World Example
  • Used by a number of unrelated classes
  • Contains a known set of methods
  • Need it to sort any type of object
  • Comparison rules known only to the sortable
    object
  • Good code reuse

2
Java API and Interfaces
  • The Java API makes heavy use of interfaces since
    they provide a powerful tool. As an example,
    consider the Arrays class defined in the
    java.util package. This class has one method
    called sort(). One of this method signature is

public static void sort(Object  a)
The method sort() sorts the specified array of
objects into ascending order, according to the
natural ordering of its elements. All elements in
the array must implement the Comparable
interface. Furthermore, all elements in the array
must be mutually comparable (that is,
e1.compareTo(e2) must not throw a
ClassCastException for any elements e1 and e2 in
the array). The Comparable represents an
interface that has to be implemented by all
classes aiming for using the sort() method of the
Arrays class. The implementing classes have to
override the following Comparator methods
int compareTo(Object o)
3
Using Interfaces
  • A sort is a classic example of the use of an
    interface. By using good OO programming technique
    and interfaces, you can eliminate all of the
    maintenance difficulties associated with the
    traditional approach.
  • The Sortable interface specifies the methods
    required to make the sort work on each type of
    object that needs to be sorted. Each class
    implements the interface based on its specific
    sorting needs. Only the class needs to know its
    object comparison.
  • The sort code is completely isolated from the
    objects that implement the sort algorithm.

4
API Sort Interface Example
  • Three classes and one interface involved in
    sorting a list of videos
  • The sortable interface declares one method
    compare(), This method must be implemented by any
    class that wants to use the sort class methods
  • The Sort class is an abstract class that contains
    sortObjects( ), a method to sort an array of
    objects. sortObjects( ) does comparison by
    calling the compare ( ) method on the objects in
    the array.
  • MyApplication rpresents any application that
    needs to sort a list of movies
  • Overview of the Classes
  • Created by the sort expert

public interface Sortable
public abstract class Sort
  • Created by the movie expert

public class Movie implements Sortable
public class MyApplication
5
Sort Interface Details

MyApplication
sortObjects() returns the sorted list
MyApplication passes an array of movies to
Sort.sortObjects()
1
4
Sort
sortObjects() asks a movie to compare itself with
another movie
2
3
The movie returns the result of the comparison
Movie
6
The Sortable Interface
The Sortable interface specifies all of the
methods and constants required for a class to be
sortable. In the example, the only method is
compare( ). Any class that implements Sortable
must provide a compare() method that accepts an
Object argument and returns an int.
  • public interface Sortable // compare()
    Compare this object to another object
  • // Returns // 0 if this object is equal
    to obj2
  • // a value lt 0 if this object lt obj2
  • // a value gt 0 if this object gt obj2
  • int compare(Object obj2)

7
The Sort Class
The sort class contains the sortObjects() method,
which sorts an array of Sortable objects,
sortObjects() accepts an array of Sortable
objects as its argument. It is legal syntax to
specify an interface type for a methods
arugument in this case it ensures that the
method will only be asked to sort objects that
implement the Sortable interface. When
sortObjects() needs to compare two items in the
array, it calls compare() on one of the items,
passing the other item as the argument
Holds sortobjects( )
public abstract class Sort public static
void sortObjects(Sortable items) // Step
through the array comparing and swapping //
do this length-1 times for (int i 1 i lt
items.length i) for (int j 0 j lt
items.length - 1 j) if
(itemsj.compare(itemsj1) gt 0)
Sortable tempitem itemsj1
itemsj1 itemsj itemsj
tempitem
8
The Movie Class
The Movie class implements the Sortable
interface. If it wants to call Sort.sortOBJECTS()
it must implement the Sortable interface, and if
it implements the Sortable interface it must
implement the compare( ) method. The compare( )
method takes an Object as an argument and
compares it with the object on which it was
called.we use the String compareTo( ) method to
compare the two title strings.
implements Sortable
public class Movie extends InventoryItem
implements Sortable String title
public int compare(Object movie2) String
title1 this.title String title2
((Movie)movie2).getTitle()
return(title1.compareTo(title2)) // End of
compare() method // End of class Movie
9
Using Sort Class
To use the sort, you simply call
Sort.sortObjects(Sortable) from your
application, passing the array of objects you
want sorted. Each object that you want to sort
must implement the Sortable interface and provide
the required compare( ) method.
Call Sort.sortObjects(Sortable ) with an array
of Movie as the argument
class myApplication Movie movielist //
build the array of Movie Sort.sortObjects(moviel
ist)
Write a Comment
User Comments (0)
About PowerShow.com