設計模式【15】| 迭代子模式

 就是一個迭代器,用來遍歷集合。java 原生代碼中的迭代器也是採用這種模式java

1.程序設計ide

出於更深刻的瞭解java迭代器原理,本身寫一遍仍是有必要的。post

       程序設計:兩個接口,兩個實現類。測試

       集合接口:構造集合的存儲結構this

       迭代器接口:用來對集合進行遍歷。spa

       本身定義一個User的model ,自定義集合,並定義添加item的方法,而後經過本身定義的迭代器,對其進行遍歷。是否是很溜呀。那咱們開始吧。這個彎子有點大,多看幾遍就明白了。設計

集合接口: NotTrueCollectioncode

package com.cn.iteractor;

public interface NotTrueCollection<T> {

	NotTrueIteractor<T> createIteractor();

	Object get(int i);

	int size();

	void addItem(T t);

	void deleteItem(T t);

} 

迭代器接口: NotTrueIteractorblog

package com.cn.iteractor;

public interface NotTrueIteractor<T> {
	
	boolean hasNext();

	Object getPrevious();

	Object next();

	Object getFirst();

	Object getLasst();
}

集合實現類:MyCollection接口

package com.cn.iteractor;

public class MyCollection<T> implements NotTrueCollection<T> {

    private Object[] collections = new Object[1];

    private int index = 0;

    @Override
    public NotTrueIteractor<T> createIteractor() {
        return new MyIteractor<T>(this);
    }

    @Override
    public Object get(int i) {
        return collections[i];
    }

    @Override
    public int size() {
        return collections.length;
    }

    @Override
    public void addItem(T t) {
        int lengths = size();
        if (index >= size()) {
            Object[] collectionTemp = collections;
            collections = new Object[lengths + 1];
            for (int i = 0; i < collectionTemp.length; i++) {
                collections[i] = collectionTemp[i];
            }
            collections[lengths] = t;
        } else {
            collections[lengths-1] = t;
        }
        index++;
    }

    @Override
    public void deleteItem(T t) {
        // TODO Auto-generated method stub

    }

}

迭代器實現類 MyIteractorimplements

package com.cn.iteractor;

public class MyIteractor<T> implements NotTrueIteractor<T> {

    private NotTrueCollection<T> notTrueCollectin;

    private int post = -1;

    public MyIteractor(NotTrueCollection<T> notTrueCollectin) {
        this.notTrueCollectin = notTrueCollectin;
    }

    @Override
    public boolean hasNext() {
        if (post < notTrueCollectin.size() - 1) {
            return true;
        }
        return false;
    }

    @Override
    public Object getPrevious() {
        if (post > 0) {
            post--;
        }
        return notTrueCollectin.get(post);
    }

    @Override
    public Object next() {
        if (post < notTrueCollectin.size() - 1) {
            post++;
        }
        return notTrueCollectin.get(post);
    }

    @Override
    public Object getFirst() {
        return notTrueCollectin.get(0);
    }

    @Override
    public Object getLasst() {
        return notTrueCollectin.get(notTrueCollectin.size() - 1);
    }
    
}

測試類: User

package com.cn.iteractor;

public class User {

    private String name;
    private String sex;
    private int age;
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", sex=" + sex + ", age=" + age + ", address=" + address + "]";
    }

}

測試結果:Test

package com.cn.iteractor;

public class Test {

    public static void main(String[] args) {
        NotTrueCollection<User> myCollection = new MyCollection<>();
        User user1 = new User();
        User user2 = new User();
        User user3 = new User();
        user1.setName("Harvey");
        user2.setName("Tom");
        user3.setName("Jack");
        myCollection.addItem(user1);
        myCollection.addItem(user2);
        myCollection.addItem(user3);
        NotTrueIteractor<User> iteractor = myCollection.createIteractor();
        while (iteractor.hasNext()) {
            User users = (User) iteractor.next();
            System.out.println(users);
        }

    }
}

 

       說明:代碼中User模型省略了get\set還有toString()方法。另外遍歷彷佛只有在遍歷集合的時候才須要,可是java 原生的代碼已經將集合遍歷完美的實現,咱們只要理解其原理就好了。

相關文章
相關標籤/搜索