分类1 适应设计模式
2019-05-09 10:14:59 0 举报
设计模式
作者其他创作
大纲/内容
adapter 适配器模式
public class BookCaseIterator { private BookCase bookcase; private int idx; public BookCaseIterator(BookCase bookcase){ this.bookcase = bookcase; this.idx = 0; } public boolean hasNext() { return this.idx < bookcase.getLength(); } public Object next() { return bookcase.get(idx++); } }
Adaptee
methodA
Target
method1
iterator 枚举模式
BookCaseIterator
aggregate: BookCase
hasNext()next()
Adapter
method1() { methodA();}
Aggregate
+ Iterator iterator()
使用委托的方案:Adaptee adaptee = new Adaptee();Target target = new Adapter(adaptee );target.method1();
Book
isbnnameauthor
BookCase (书柜)
Book[] books
Iterator iterator() { return new BookCaseIterator(this);}Book get(int idx) { return books[idx];}
Adaptee adaptee;
method1() { adaptee.methodA();}
使用继承的方案:Target target = new Adapter();target.method1();
Iterator
+ boolean hasNext()+ Object next()
0 条评论
下一页