国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

談?wù)勗O(shè)計模式中的Iterator迭代器

2019-11-18 12:17:34
字體:
供稿:網(wǎng)友

  在Pet Store中的CatalogDao使用了DAO模式,從而完成Fast-Lane Reader模式,以便能快速的輸出數(shù)據(jù)庫元素
  
  列表,同時使用for page-by-page iteration完成每頁的輸出顯示。
  
  在CatalogDAOImpl 中基本返回的是Page,也就是說,在CatalogDAOImpl的具體JDBC數(shù)據(jù)庫查詢時,就將Page功
  
  能融入其中,從而一步到位的完成輸出顯示。
  
  但在實際系統(tǒng)應(yīng)用中,我們都有用戶權(quán)限約束,也就是說,并不是每條數(shù)據(jù)庫記錄都能被顯示輸出,有些用戶
  
  就只能看到他被授權(quán)看到的的記錄。
  
  Jive中的Iterator模式就很好的解決了這個問題,Jive中使用PRoxy模式完成用戶權(quán)限級別的驗證,同時為了更
  
  快的獲得數(shù)據(jù)庫記錄和節(jié)約內(nèi)存,Jive專門建立了自己的Iterator模式,這些一開始讓人迷惑,直接使用
  
  Collection的Iterator不是更好,雖然簡單方便了,但是前提是在內(nèi)存中要先開辟一塊Collection內(nèi)存,假如
  
  數(shù)據(jù)庫記錄很大,將耗費很多內(nèi)存,致使系統(tǒng)癱瘓(細節(jié)討論見
  http://www.jdon.com:81/jive/thread.jsp?forum=16&thread=302)
  
  Jive的Iterator不只是傳遞了數(shù)據(jù)庫指針,而且加載了權(quán)限驗證功能,因此,這一模式是實用可行的,那么在
  
  我們自己的EJB應(yīng)用中如何綜合這兩個系統(tǒng)的模式優(yōu)點?
  
  這其中應(yīng)該有很多中間方案可行,假如你有愛好可以貼出你的想法,我目前采取的是DAO模式和Jive的Iterator
  
  模式集合,也就是說,在自己的EJB中不直接返回Page 而是返回Iterator,這個Iterator是類似Jive中的
  
  DatabaSEObjectIterator。
  
  簡單的說,由于Jive不是EJB架構(gòu),所以,將Jive中的訪問數(shù)據(jù)庫段用DAO模式替代,其他都可以照搬Jive的
  
  Iterator模式,關(guān)于前端JSP頁面的分頁輸出,這時可以參考Pet Store的page-by-page iteration模式,也就
  
  是說,根據(jù)Iterator模式再拓展寫Page,結(jié)構(gòu)和功能類似Pet store的Page.
  
  這里只提供一個大體思路,假如要寫透徹真是很長,看看平常我們以前用asp php做的數(shù)據(jù)庫查詢分頁的簡單功
  
  能蘊含這么多新的思想,其實這些思想也是在ASP PHP應(yīng)付大數(shù)據(jù)庫量失敗的總結(jié),所以軟件質(zhì)量控制是顯得多
  
  么重要。
  
  
  
  我用Iterator的程序代碼:
  public interface DataListIterator
  {
  /**
  * 功能類似于java.util.Iterator.hasNext()
  *
  * @return 假如有下一個元素,返回true
  * @throws Exception
  */
  public boolean hasNext() throws Exception;
  
  /**
  * 功能類似于java.util.Iterator.next(),但是返回的是數(shù)據(jù)庫查詢的結(jié)果
  * 的字段值字符串數(shù)組。
  *
  * @return String[] 字段值字符串數(shù)組
  * @throws Exception
  */
  public String[] next() throws Exception;
  }
  
  
  
  public interface DataList{
  
  /**
  * 取出指定位置查詢結(jié)果中的字段值,放到一個字符串數(shù)組中并返回。
  * 功能類似于java.util.List.get(int)
  *
  * @param index 查詢結(jié)果的索引
  * @return String[] 結(jié)果中的字段值數(shù)組
  *
  * @throws Exception
  */
  public String[] get(int index) throws Exception;
  
  /**
  * 檢查查詢結(jié)果的集和是否為空集合
  *
  * @return boolean true表示空集合
  * @throws Exception
  */
  public boolean isEmpty() throws Exception;
  
  /**
  * 檢查是否還有下一個查詢結(jié)果
  *
  * @return boolean true表示有下一個
  * @throws Exception
  */
  public boolean hasNext() throws Exception;
  
  /**
  * 檢查在指定位置上是否有查詢結(jié)果
  *
  * @param index 查詢結(jié)果的索引
  * @return boolean true表示有查詢結(jié)果
  * @throws Exception
  */
  public boolean isElementExist(int index) throws Exception;
  
  /**
  * 把游標放到指定的位置上,功能類似于java.sql.ResultSet.absolute(int)
  *
  * @param index 指定的位置,從0開始
  * @return boolean true表示操作成功
  * @throws Exception
  */
  public boolean absolute(int index) throws Exception;
  
  /**
  * 把游標放到查詢結(jié)果的最前面,功能類似于java.sql.ResultSet.beforeFirst()
  *
  * @throws Exception
  */
  public void beforeFirst() throws Exception;
  
  /**
  * 把游標放到查詢結(jié)果的第一個,功能類似于java.sql.ResultSet.first()
  *
  * @return boolean true表示移動成功
  * @throws Exception
  */
  public boolean first() throws Exception;
  
  /**
  * 把游標放到查詢結(jié)果的最后一個,功能類似于java.sql.ResultSet.last()
  *
  * @return boolean true表示移動成功
  * @throws Exception
  */
  public boolean last() throws Exception;
  
  /**
  * 取得整個查詢結(jié)果的大小,功能類似于java.util.List.size()
  *
  * @return size 查詢結(jié)果的大小
  * @throws Exception
  */
  public int size() throws Exception;
  
  /**
  * 提供一個可以遍歷查詢結(jié)果的對象,功能類似于java.util.List.iterator()
  *
  * @return DataListIterator 可以遍歷查詢結(jié)果的對象
  * @throws Exception
  */
  public DataListIterator iterator() throws Exception;
  
  
  }
  
  
  
  
  
  
  
  
  public interface DataListHandler{
  
  /**
  * 得到查詢結(jié)果的一個子集
  *
  * @param startIndex 子集的起始位置
  * @param count 子集的個數(shù)
  * @return Datalist 返回一個子集
  * @throws Exception
  */
  public DataList getListChunk(int startIndex, int count) throws Exception;
  
  /**
  * 取得整個查詢結(jié)果的大小,功能類似于java.util.List.size()
  *
  * @return size 查詢結(jié)果的大小
  * @throws Exception
  */
  public int size() throws Exception;
  
  /**
  * 檢查子集的前面是否還有查詢結(jié)果
  *
  * @return boolean true表示前面還有查詢結(jié)果
  */
  public boolean hasPrevious();
  
  /**
  * 檢查子集的后面是否還有查詢結(jié)果
  *
  * @return boolean true表示后面還有查詢結(jié)果
  * @throws Exception
  */
  public boolean hasNext() throws Exception;
  
  /**
  * 關(guān)閉對象
  * @throws Exception
  */
  public void close() throws Exception;
  }
  
  
  
  
  
  
  
  * @version 1.0
  *
  * Page實現(xiàn)了DataListHandler。
  *
  * 用于操作ResultSetDataList對象,對查詢結(jié)果集進行分頁顯示。在進行顯示的期間,必須
  * 保持數(shù)據(jù)庫連接Connection和結(jié)果集ResultSet沒有關(guān)閉。
  * 基本使用方法舉例:
  *
  
   * int index = 4, count = 10;//顯示第5到第14條記錄
   * Connection conn = Pool.getConnection();
   * //(1)使用一個QueryDAO的具體子類來初始化頁對象
   * ResultSetQueryDAO dao = new ResultSetQueryDAO();
   * ResultSetPage page = new ResultSetPage(conn, dao);
   *
   * //(2)或者直接使用一個ResultSet對象來初始化頁對象
   * //ResultSet rs = ...;
   * //ResultSetPage page = new ResultSetPage(conn, rs);
   *
   * //需要顯示的當前頁chunk
   * DataList chunk = page.getListChunk(index, count);
   * //當前頁的前后是否還有記錄,用于顯示PRVEIOUS和NEXT按鈕
   * boolean hasPrevious = page.hasPrevious();
   * boolean hasNext = page.hasNext();
   * //遍歷顯示當前頁的記錄
   * DataListIterator it = chunk.iterator();
   * while (it.hasNext())
   * {
   * String[] valuesOfRow = it.next();
   * for(int i = 0; i < valuesOfRow.length; i++)
   * System.out.println(valuesOfRow[i]);
   * }
   *
   *
  
  *
  * @see QueryDAO
  * @see DataListIterator
  */
  
  import java.sql.*;
  
  public class ResultSetPage implements DataListHandler{
  
  private Connection conn = null;
  private ResultSet rs = null;

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 沅陵县| 太康县| 沙坪坝区| 碌曲县| 东明县| 正定县| 武清区| 德化县| 登封市| 沙洋县| 清苑县| 徐水县| 北碚区| 新宁县| 山东省| 加查县| 涿州市| 永善县| 龙游县| 富裕县| 山阳县| 永修县| 邯郸县| 临沂市| 江安县| 观塘区| 闽清县| 开平市| 普定县| 高雄市| 绥滨县| 宿州市| 盖州市| 侯马市| 六枝特区| 津市市| 正阳县| 攀枝花市| 商都县| 满洲里市| 双江|