Tải bản đầy đủ (.pptx) (45 trang)

Abstract with interfaces (lập TRÌNH cơ bản SLIDE)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (236.37 KB, 45 trang )

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 1 + this.rest.count();
}


Manipulating in abstract list (cont)
• Q: Comparing the two contains methods from the
problem with stations and routes ,it is clear that:

– The same code is used to determine whether a list of
Routes contains a given Route as the code to determine
whether a list of Stations contains a given Station.
– The only difference is in the method header: in one case
the method parameter is an instance of Route, in the other
case it is an instance of Station.
– However, any instance of Route and any instance of Station
is also of type Object


Manipulating in abstract list (cont)
• // Class ALoObject
public abstract boolean contains(Object that);
• // Class MTLoObject
public boolean contains(Object that) {
return false;
}      
• // Class ConsLoObject
public boolean contains(Object that) {
return this.first.equals(that) || this.rest.contains(that);
}


Exercise
• Exercise 1.0.1.   Design a test suite that will
define several lists of Objects that contain only
Book objects and use them to test the
methods count and contains.
• Do the same for lists of Shape objects.
• Exercise 1.0.2.   Develop the method remove

that removes a given object from a list of
objects. Test your method on the classes of
Books, and classes of Shapes.


Abstracting over Data Definitions
with Interfaces
(part 1)


Class diagram
<<abstract>>
ALog
+double totalDistance()

self-referential

Entry
MTLog
+double totalDistance()

ConsLog
-Entry first
-ALog rest
+double totalDistance()

-Date date
-double distance
-double duration
-String feeling



Class diagram
<<abstract>>
ALoBook
+double totalPrice()
self-referential
Book
MTLoBook
+double totalPrice()

ConsLoBook
-Book first
-ALoBook rest
+double totalPrice()

-Author author
-String title
-int publishYear
-double price


Take a look on code
public class Entry {
private Date date;
private double distance;
private int durationInMinutes;
private String postRunFeeling;
}


// class ALog
public abstract double totalDistance(){
}
//class MTLog
public double totalDistance() {
return 0.0;
}
//class ConsLog
public double totalDistance() {
return this.first.getDitance() +
this.rest.totalDistance();
}
}

public class Book {
private Author author;
private String title;
private int publishYear;
private double price;
}

// class ALoBook
public abstract double totalPrice(){
}
//class MTLoBook
public double totalPrice() {
return 0.0;
}
//class ConsLoBook
public double totalPrice() {

return this.first.getPrice() +
this.rest.totalPrice();
}
}


Take note
• The structure of the methods is the same, that
measures some total value of all items in the list.
– The value in Entry is the distance
– The valuein Book is the price of a book

• However, the name of the field in each class that
extracts the value of the individual item is different in
each case.
– It is posible to ask each class to provide getValue method
that would return the desired value ?

• We could rename the method to totalValue. The
resulting methods are almost the same.


Take a look on code
//Class Entry
public double getValue() {
return this.distance;
}
// class ALog
public abstract double totalValue(){
}

//class MTLog
public double totalValue() {
return 0.0;
}
//class ConsLog
public double totalValue() {
return this.first.getValue() +
this.rest.totalValue();
}
}

//Class Book
public double getValue() {
return this.price;
}
// class ALoBook
public abstract double totalValue(){
}
//class MTLoBook
public double totalValue() {
return 0.0;
}
//class ConsLoBook
public double totalValue() {
return this.first.getValue() +
this.rest.totalValue();
}
}



Abstract with Object
• We can replace a list of Book and a list of Entry by a list of Object.
// class ALoObject
public abstract double totalValue(){
}
//class MTLoObject
public double totalValue() {
return 0.0;
}
//class ConsLoObject
Public double totalValue() {
return this.first.getValue() + this.rest.totalValue();
}
}


Take note
• The program code in the class ConsLoObject
references this.first.getValue(). But the class
Object does not define the method double
getValue().
• Q: How to solve this problem?


Solution
• Naturally, difference objects can take the same
role in some operation (method).
• In OOP, we define a interface that declare this
same role (method).
• Each class can implements this interface and

do the concrete method


Solution (cont)


In our case, we define Interface IValuable that declares double
getValue() method.

public interface IValuable {
public double getValue();
}
• Then we make Entry and Book implements IValuable and do their
concrete method.
public class Entry implements Ivaluable {

public class Book implements Ivaluable {

….

….

public double getValue(){

public double getValue(){

return this.distance;

return this.price;


}
}

}
}


Exercise
• Exercise 2.0.3.   Design the tests for the method totalValue
that will consume a list of Books.
• Design a similar test for a list of Shape objects
• Exercise 2.0.4.   Develop the method onlyTheBest that
creates a new list of objects, selecting only those that have a
value greater than some threshold specified by the user.
• Exercise 2.0.5.   Develop the method hasValue that
determines whether there is an item in this list that has a
value lower than some value specified by the user.
• Exercise 2.0.6.   Develop the method allFine that determines
whether all objects in this list have a value greater than some
threshold specified by the user.


Relax &

…Do Exercises …
Too much hard exercises now
Try again, never stop practicing!

GOOD JOB!



×