乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      迭代器模式一訪問人員列表

       WindySky 2018-02-25

      一模式定義

      迭代器模式,提供了一種模式順序訪問一個集合對象中各個元素的功能,而又不暴露其內部的表示。

       

      二模式舉例

      1模式分析

      我們借用訪問人員列表這一案例來說明這一模式。

      2迭代器模式靜態(tài)類圖



       

      3代碼示例

      3.1 人員信息接口——IPerson

      Java代碼  收藏代碼
      1. package com.demo.person;  
      2.   
      3. /** 
      4.  * 人員信息 
      5.  *  
      6.  * @author 
      7.  *  
      8.  */  
      9. public interface IPerson  
      10. {  
      11.     /** 
      12.      * 獲得人員信息 
      13.      *  
      14.      * @return 
      15.      */  
      16.     public String getPersonInfo();  
      17.   
      18. }  

       

      3.2 人員信息實現(xiàn)——Person

      Java代碼  收藏代碼
      1. package com.demo.person;  
      2.   
      3. /** 
      4.  * 人員具體實現(xiàn)類 
      5.  *  
      6.  * @author 
      7.  *  
      8.  */  
      9. public class Person implements IPerson  
      10. {  
      11.     // 姓名  
      12.     private String name;  
      13.     // 年齡  
      14.     private int age;  
      15.     // 性別(1:男性 0:女性)  
      16.     private int sex;  
      17.   
      18.     /** 
      19.      * 構造方法設置屬性內容 
      20.      *  
      21.      * @param name 
      22.      * @param age 
      23.      * @param sex 
      24.      */  
      25.     public Person(String name, int age, int sex)  
      26.     {  
      27.         this.name = name;  
      28.         this.age = age;  
      29.         this.sex = sex;  
      30.     }  
      31.   
      32.     /** 
      33.      * 獲得人員信息 
      34.      *  
      35.      * @return 
      36.      */  
      37.     public String getPersonInfo()  
      38.     {  
      39.         return "姓名:" + this.name + " - 年齡:" + this.age + " - 性別:" + (this.sex == 1 ? "男" : (this.sex == 0 ? "女" : ""));  
      40.     }  
      41.   
      42. }  

       

      3.3 人員集合接口——IPersonList

      Java代碼  收藏代碼
      1. package com.demo.person;  
      2.   
      3. import com.demo.iterator.Iterator;  
      4.   
      5. /** 
      6.  * 人員接口 
      7.  *  
      8.  * @author 
      9.  *  
      10.  */  
      11. public interface IPersonList {  
      12.     /** 
      13.      * 獲得內部存儲人員信息內容 
      14.      *  
      15.      * @return 
      16.      */  
      17.   
      18.     public IPerson[] getPersonList();  
      19.   
      20.     /** 
      21.      * 迭代器 
      22.      *  
      23.      * @return 
      24.      */  
      25.     public Iterator iterator();  
      26. }  

       

      3.4 人員集合實現(xiàn)——PersonList

      Java代碼  收藏代碼
      1. package com.demo.person;  
      2.   
      3. import java.util.Random;  
      4.   
      5. import com.demo.iterator.ArrPersonIterator;  
      6. import com.demo.iterator.Iterator;  
      7.   
      8. /** 
      9.  * 人員列表信息 
      10.  *  
      11.  * @author 
      12.  *  
      13.  */  
      14. public class PersonList implements IPersonList {  
      15.     // 存儲用戶信息列表  
      16.     private final IPerson[] list = new IPerson[10];  
      17.   
      18.     // 構造方法初始化人員信息  
      19.     public PersonList() {  
      20.         Random random = new Random();  
      21.         // 創(chuàng)建人員信息  
      22.         for (int i = 0; i < 10; i++) {  
      23.             IPerson person = new Person("人員" + i, random.nextInt(30), random  
      24.                     .nextInt(2));  
      25.             list[i] = person;  
      26.             // this.list.add(person);  
      27.         }  
      28.     }  
      29.   
      30.     /** 
      31.      * 獲得內部存儲人員信息內容 
      32.      *  
      33.      * @return 
      34.      */  
      35.   
      36.     public IPerson[] getPersonList() {  
      37.         return list;  
      38.     }  
      39.   
      40.     /** 
      41.      * 迭代器 
      42.      *  
      43.      * @return 
      44.      */  
      45.     public Iterator iterator() {  
      46.         return new ArrPersonIterator(this.list);  
      47.     }  
      48. }  

       

      3.5 迭代器接口——Iterator

      Java代碼  收藏代碼
      1. package com.demo.iterator;  
      2.   
      3. /** 
      4.  * 迭代器接口 
      5.  *  
      6.  * @author 
      7.  *  
      8.  */  
      9. public interface Iterator {  
      10.   
      11.     // 判斷是否含有下一個屆節(jié)點  
      12.     public boolean hasNext();  
      13.   
      14.     // 獲得下一個節(jié)點對象  
      15.     public Object next();  
      16.   
      17.     // 刪除對象  
      18.     public Object remove();  
      19. }  

       

      3.6 迭代器實現(xiàn)——ArrPersonIterator

      Java代碼  收藏代碼
      1. package com.demo.iterator;  
      2.   
      3. import com.demo.person.IPerson;  
      4.   
      5. /** 
      6.  * 數(shù)組迭代器實現(xiàn) 
      7.  *  
      8.  * @author 
      9.  *  
      10.  */  
      11. public class ArrPersonIterator implements Iterator {  
      12.     // 私有屬性存儲人員列表對象信息  
      13.     private final IPerson[] personList;  
      14.     // 存儲位置信息 初始值為-1  
      15.     private int index = -1;  
      16.   
      17.     /** 
      18.      * 構造方法將人員列表對象傳入 
      19.      *  
      20.      * @param personList 
      21.      */  
      22.     public ArrPersonIterator(IPerson[] personList) {  
      23.         this.personList = personList;  
      24.     }  
      25.   
      26.     // 判斷是否含有下一個節(jié)點  
      27.     public boolean hasNext() {  
      28.         return (this.personList == null ? false  
      29.                 : (index < this.personList.length - 1));  
      30.     }  
      31.   
      32.     // 獲得下一個節(jié)點對象  
      33.     public Object next() {  
      34.         if (this.personList != null && (index < this.personList.length - 1)) {  
      35.             // 獲得人員列表對象中的人員信息  
      36.             return this.personList[++index];  
      37.         }  
      38.         return null;  
      39.     }  
      40.   
      41.     // 刪除對象  
      42.     public Object remove() {  
      43.         if (this.personList != null) {  
      44.             IPerson person = this.personList[index];  
      45.             this.personList[index] = null;  
      46.             return person;  
      47.         }  
      48.   
      49.         return null;  
      50.     }  
      51.   
      52. }  

       

      3.7 讓迭代器遍歷集合對象——Client

      Java代碼  收藏代碼
      1. package com.demo;  
      2.   
      3. import com.demo.iterator.Iterator;  
      4. import com.demo.person.IPerson;  
      5. import com.demo.person.IPersonList;  
      6. import com.demo.person.PersonList;  
      7.   
      8. public class Client {  
      9.   
      10.     /** 
      11.      * @param args 
      12.      */  
      13.     public static void main(String[] args) {  
      14.         // 創(chuàng)建人員列表對象  
      15.         IPersonList personList = new PersonList();  
      16.         System.out.println("----使用迭代器輸出人員信息 start......");  
      17.         // 生成迭代器  
      18.         Iterator iterator = personList.iterator();  
      19.         // 循環(huán)迭代器 遍歷每一個元素輸出人員信息  
      20.         while (iterator.hasNext()) {  
      21.             // 獲得人員對象實例  
      22.             IPerson person = (IPerson) iterator.next();  
      23.             if (person != null) {  
      24.                 // 輸出人員信息  
      25.                 System.out.println(person.getPersonInfo());  
      26.             }  
      27.         }  
      28.         System.out.println("----使用迭代器輸出人員信息   end......");  
      29.   
      30.     }  
      31. }  

       

      4運行結果

      ----使用迭代器輸出人員信息 start......

      姓名:人員0 - 年齡:28 - 性別:女

      姓名:人員1 - 年齡:25 - 性別:女

      姓名:人員2 - 年齡:23 - 性別:女

      姓名:人員3 - 年齡:18 - 性別:女

      姓名:人員4 - 年齡:27 - 性別:女

      姓名:人員5 - 年齡:28 - 性別:男

      姓名:人員6 - 年齡:25 - 性別:女

      姓名:人員7 - 年齡:23 - 性別:女

      姓名:人員8 - 年齡:16 - 性別:男

      姓名:人員9 - 年齡:12 - 性別:女

      ----使用迭代器輸出人員信息   end......

       

      三該模式設計原則

      1"開一閉"原則

      2單一職責原則

       

      四使用場合

      1訪問一個集合對象的內容,而無須暴露它的內部表示。

      2支持對集合對象的多種遍歷方式。

      3為遍歷不同的集合對象結構提供一個統(tǒng)一的接口。

       

      五迭代器模式靜態(tài)類圖



       

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多