Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.

Similar presentations


Presentation on theme: "1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements."— Presentation transcript:

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

2 2 Role of Interfaces in Large-Scale Software 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: 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) public static void sort(Object [] a)

3 3 Role of Interfaces in Large-Scale Software Using Interfaces l 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. l 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. l The sort code is completely isolated from the objects that implement the sort algorithm.

4 4 Role of Interfaces in Large-Scale Software API Sort Interface Example l Three classes and one interface involved in sorting a list of videos l The sortable interface declares one method: compare(), This method must be implemented by any class that wants to use the sort class methods l 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. l MyApplication rpresents any application that needs to sort a list of movies Overview of the Classes public interface Sortable public abstract class Sort public class Movie implements Sortable public class MyApplication Created by the sort expert Created by the movie expert

5 5 Role of Interfaces in Large-Scale Software Sort Interface: Details MyApplication 1 MyApplication passes an array of movies to Sort.sortObjects() Sort Movie sortObjects() asks a movie to compare itself with another movie 2 The movie returns the result of the comparison 4 sortObjects() returns the sorted list 3

6 6 Role of Interfaces in Large-Scale Software The Sortable Interface public interface Sortable { // compare(): Compare this object to another object // Returns: // 0 if this object is equal to obj2 // a value < 0 if this object < obj2 // a value > 0 if this object > obj2 int compare(Object obj2); } 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.

7 7 Role of Interfaces in Large-Scale Software 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 method’s 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 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 < items.length; i++) { for (int j = 0; j < items.length - 1; j++) { if (items[j].compare(items[j+1]) > 0) { Sortable tempitem = items[j+1]; items[j+1] = items[j]; items[j] = tempitem; } } } } } Holds sortobjects( )

8 8 Role of Interfaces in Large-Scale Software 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. 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 implements Sortable

9 9 Role of Interfaces in Large-Scale Software 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. class myApplication { Movie[] movielist; … // build the array of Movie Sort.sortObjects(movielist); } Call Sort.sortObjects(Sortable []) with an array of Movie as the argument


Download ppt "1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements."

Similar presentations


Ads by Google