Special java subject - Part 3

Problem in designing a common list -We need to design a common list used for both list of books and list of entry. -Problem: nThe list contains only concrete type of object (book or entry) -Solution: nAbstracting over data definitions with Abstract class (Object polymorphism) nAbstracting over data definitions with Interface

ppt21 trang | Chia sẻ: tlsuongmuoi | Lượt xem: 2580 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Special java subject - Part 3, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
INTERFACE AND INNER CLASS SPECIAL JAVA SUBJECT Problem in designing a common list We need to design a common list used for both list of books and list of entry. Problem: The list contains only concrete type of object (book or entry) Solution: Abstracting over data definitions with Abstract class (Object polymorphism) Abstracting over data definitions with Interface Class Diagram: Log of Entries Class diagram: List of books New Class Diagram: Log of Entries New Class diagram: List of books Modified Class Diagram using Abstract Class Modified Class Diagram using Interface What is an Interface An interface defines: a protocol of behavior that can be implemented by any class anywhere in the class hierarchy; a set of methods but does not implement them. A class that implements the interface agrees to implement all the methods defined in the interface, thereby agreeing to certain behavior. Definition: An interface is a named collection of method definitions, without implementations. What is an Interface Because an interface is simply a list of unimplemented, and therefore abstract, methods, you might wonder how an interface differs from an abstract class. The differences are significant. An interface cannot implement any methods, whereas an abstract class can. A class can implement many interfaces but can have only one superclass. An interface is not part of the class hierarchy. Unrelated classes can implement the same interface. Steps in using a Interface define an INTERFACE that defines the method header for the desired method, declare in each class that defines this method that it IMPLEMENTS THE INTERFACE and CAST the object that invokes the desired method to be of the type for which the desired method is defined. Interface implement We define the IValuable interface as follows: interface IValuable{ double value(); } In the classes Entry and Book we define the method double value() and change the headers to indicate that these classes implement the IValuable interface: class Entry implements IValuable{... class Book implements IValueable{... Finally, in the class ConsLoObject we modify the code for the method totalvalue() by using the cast to require that the object this.fst is of the type that implements the IValuable interface: double totalvalue() { return ((IValuable)this.fst).value() + this.rst.totalvalue(); Implement a Sorted list with Interface Consider the following problem The bookstore manager would like to have a list of books ordered by the published year. The runner would like to have a list of entries ordered by the date We need to develop the method sort in the class AList and its derived classes. abstract public AList sort(); Implement a Sorted list with Interface Implement a Sorted list with Interface We need to complete the method insert. // in the class MTList : insert the given book into this sorted list of books AList insert(Object b){ return new List(b, this); } // to create from this list a new list sorted by the year published AList sort(){ return this;} // in the class List : insert the given book into this sorted list of books AList insert(Object b){ if ( b interface Comparable{ // compare this object with the given object // produce negative result if this is 'smaller' than the given object // produce zero if this object is 'the same' as the given object // produce positive result if this is 'greater' than the given object int compareTo(T obj); } Using the Comparable Interface public class Entry implements IValuable, Comparable{ private Date date; private String comment; private double distance; private int duration; // compare the date recorded in this entry with the given entry public int compareTo(Entry obj) { return this.date.getDate() - obj.date.getDate(); } } Using the Comparable Interface public class Book implements IValuable, Comparable{ private Author author; private String title; private double cost; private int publishYear; // compare the publication year of this book with the given book public int compareTo(Book obj) { return this.publishYear - obj.publishYear; } } Using the Comparable Interface public class List extends AList { private Object first; private AList rest; public List(Object first, AList rest) { this.first = first; this.rest = rest; } public AList sort() { return this.rest.sort().insert(this.first); } public String toString() { return "" + first + rest; } AList insert(Object obj) { if (((Comparable)this.first).compareTo(obj) > 0) return(new List(obj, this)); else return(new List(this.first, this.rest.insert(obj))); }} Objects that represent functions interface Comparator{ int compare(Object obj1, Object obj2); }

Các file đính kèm theo tài liệu này:

  • pptSPECIAL JAVA SUBJECT part 3.ppt
Tài liệu liên quan