Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 45 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
45
Dung lượng
256,42 KB
Nội dung
Chapter 06 – part VI Abtraction with interfaces Abstracting over data definitions with object Problem Many of the class hierarchies we have seen look alike A List of Books is one of: - a empty - a structure of - a Book - a List of Books A Book contains: - title price A List of Authors is one of: - a empty - a structure of - a Author - a List of Authors A Author contains: - name - dayOfBirth Note • The data definitions for lists of books, lists of authors, lists of train routes, or lists of stations all look alike, because we derived them from data definitions that followed the same pattern • Many methods for these classes look alike as well and in some cases are completely identical – For example, the code for counting the number of items in a list looks the same for all kinds of lists – The methods to determine whether a list contains some item is similar for all lists • Repeating the same code is a major source of errors in programs – By observing the similarities and designing abstractions that represent the common behavior we provide a single point of control – Additionally, the more general code may be reused in the future in solving similar problems Take a look on code //Class ALoBooks public abstract class ALoBooks { } //Class Empty public class Empty extends ALoBooks { } //Class Cons public class Cons extends ALoBooks { private Book first; private ALoBooks rest; public Cons(Book first, ALoBooks rest) { this.first = first; this.rest = rest; } //Class ALoAuthors public abstract class ALoAuthors { } //Class Empty public class Empty extends ALoAuthors { } //Class Cons public class Cons extends ALoAuthors { private Author first; private ALoAuthors rest; public Cons(Author first, ALoAuthors rest){ this.first = first; this.rest = rest; } Note • Comparing the definitions and identify the differences, we see: – The only difference between the data definitions for the lists is the type of the object that the list contains – Because every class in Java extends the class Object we could define instead a list of Objects Abstracting over data definitions with object The data definition for the Java classes is: public abstract class ALoObject { } public class MTLoObject { MTloObject() { } } public class ConsLoObject { private Object first; privet ALoObject rest; ConsLoObject( Object first, ALoObject rest){ this.first = first; this.rest = rest; } } Validating for every list • We now must validate that every list from our original examples can be represented as a list of Objects • // example for a list of book Book b1 = new Book("Call of the Wild", 10); Book b2 = new Book("The Sea-Wolf", 12); Book b3 = new Book("HtDP", 55); ALoObject blist = new ConsLoObject(b2, new ConsLoObject(b3, new MTLoObject())); Validating for every list • // example for a list of Author Author jackL = new Author("Jack London", 1876); Author dsteel = new Author("Danielle Steel", 1955); ALoObject authors = new ConsLoObject(jackL, new ConsLoObject(dsteel, new MTLoObject())); Manipulating in abstract list • Q: count the number of objects in the list • // Class ALoObject public abstract int count(); • // Class MTLoObject public int count() { return 0; } • // Class ConsLoObject public int count() { return + this.rest.count(); } Interface Comparable • In general, here we want to compare some information recorded in the this.first object with similar information recorded in the object that is the argument to this function • Java libraries include the interface Comparable defined as follows: 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 (Object obj); } • Obviously, it is up to the implementing class to decide how to compare two items Let us implement the compareTo method for the class Book and for the class Entry Example Entry e1 = new Entry(d1, 5.0, 25,"Good"); Entry e2 = new Entry(d2, 3.0, 24,"Tired"); Entry e3 = new Entry(d3, 26.0, 156,"Great"); e1.compareTo(e2); // should be > e2.compareTo(e3); // should be < e2.compareTo(e2); // should be = compareTo in class Entry • In the class Entry we modify the purpose, but the header must be the same as defined in the interface: • //compare the distance recorded in this entry with the given entry public class Entry implements IValuable, Comparable { … int compareTo(Object obj){ return this.distance - ((Entry)obj).distance; } } Examples Book b1 = new Book("HtDP", 2000, 55); Book b2 = new Book("Call of the Wild", 1995, 10); Book b3 = new Book("The Sea-Wolf", 1999, 12); b1.compareTo(b2); // should be > b2.compareTo(b3); // should be < b2.compareTo(b2); // should be = compareTo in class Book • In the class Book we modify the purpose, but the header must be the same as defined in the interface: • // compare the publication year of this book with the given book public class Book implements IValuable, Comparable { … int compareTo(Object obj){ return this.year - ((Book)obj).year; } } Abstract with Object //Class ALoObject public abstract ALoObject sort(); protected abstract ALog insert(Object that); //Class MTLoObject public ALoObject sort() { return this; } protected ALoObject insert(Object that) { return new ConsLoObject (that, this); } //Class ConsLoObject public ALoObject sort() { return this.rest.sort().insert(this.first); } protected ALoObject insert(Object that) { if (that.compareTo(this.first)