class Reader{
static final int BOOK = 1,EBOOK = 2;
public void read(int page, int bookType){
if(bookType = BOOK){
Book book = new Book("Harry Potter");
book.read(page);
}else if(bookType = EBOOK){
EBook book = new EBook("Harry Potter");
book.read(page);
}
}
}
class Reader{
private IBook book;
public void read(int page){
book.read(page);
}
public Reader(IBook book){
this.book = book;
}
}
class Book implement IBook{...}
class EBook implement IBook{...}
public class TestBook implement IBook(){
public bool readed = false;
public void read(int page){
readed = true;
}
}
//測試案例:
public void ReaderTest(){
TestBook book = new TestBook()
Reader reader = new Reader(book);
reader.read();
assertTrue(book.readed);
}