我正在解决以下问题:
迭代器设计模式是一种封装性很强的设计模式。举个例子;图书馆需要一个图书管理系统。一堂课为book
s 存储他们的详细信息和一个类library
存储书籍和书架编号。假设图书馆希望使用以下方式将数据存储在数据库中JDBC
.
如何使用 JDBC 实现迭代器设计模式以确保数据的封装?
我关心的是数据库的处理位置以及数据如何在应用程序之间共享。
数据库处理程序可以是库类的内部类吗?那么是否可以保存数据并根据请求检索数据而不影响封装?
我还在学习,还有很长的路要走,所以要温柔:)
这是关于使用迭代器模式访问数据库的近似。
package tk.ezequielantunez.stackoverflow.examples;
import java.util.Collection;
import java.util.Iterator;
/**
* Paged implementatios of a DAO. Iterator interface is used.
* Nottice you get pages of Collections, where resides your data.
* @author Ezequiel
*/
public class DAOIterator implements Iterator<Collection> {
int actualPage;
int pageSize;
public DAOIterator(int pageSize) {
this.actualPage = 0;
this.pageSize = pageSize;
}
/**
* Indicates if you have more pages of datga.
*/
@Override
public boolean hasNext() {
return actualPage < getTotalPages();
}
/**
* Gets the next page of data.
*/
@Override
public Collection next() {
return getPage(++actualPage);
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Calculates total number of pages.
*/
private int getTotalPages() {
/* You could do a count of total results and divide them by pageSize */
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Get a page of results with X objects, where X is the pageSize used in
* constructor.
*/
private Collection getPage(int page) {
/* Get data from database here */
throw new UnsupportedOperationException("Not supported yet.");
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)