ITERATOR 迭代子模式

来源:互联网 发布:中国安检有多严格 知乎 编辑:程序博客网 时间:2024/05/16 06:35
迭代子模式:可以顺序的访问一个聚集中的元素而不必暴露聚集的内部表象


如果聚集的接口提供了可以用来修改聚集元素的方法,这个接口就是所谓的宽接口。这种提供宽接口的聚集叫  “白箱聚集”,这时候的迭代子叫游标迭代子或 外禀迭代子。



改良的设计 :


在Java语言中,实现双重接口的办法就是将迭代子类设计成聚集类的内部成员类,这样迭代子对象就可以像聚集对象的内部成员一样访问聚集对象的内部结构了。这种同时保证聚集对象的封装和迭代子功能的实现的方案叫做 "黑箱"实现方案. 这时候的迭代子在 聚集的内部,因此又叫 “内禀迭代子"


(1)  白箱聚集  和  外禀迭代子



/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:23:30
 * <p/>
 * note: 白盒聚集与外禀迭代子 聚集角色
 */
abstract public class Aggregate {

    /**
     * 迭代子模式要求 聚集对象 必须有一个 工厂方法
     * 向外界提供迭代子对象实例
     * @return
     */
    public Iterator createIterator() {
        return null;
    }
}



/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:24:04
 * <p/>
 * note: 白盒聚集与外禀迭代子 具体聚集角色
 */
public class ConcreteAggregate extends Aggregate {
    private Object objs[]= {"Monk Tang",
        "Monkey", "Pigsy",
        "Sandy", "Horse"};

    /**
     *
     * @return
     */
    public Iterator createIterator() {
        return new ConcreteIterator(this);
    }

    /*
     * 白箱聚集向外界提供了访问自己内部元素的接口
     * 从而使外禀迭代子调用这个方法实现 迭代
     */
    public Object getElement(int index) {
        if (index < objs.length) {
            return objs[index];
        } else {
            return null;
        }
    }

    public int size() {
        return objs.length;
    }
}


/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:25:12
 * <p/>
 * note: 白盒聚集与外禀迭代子: 迭代子角色
 */
public interface Iterator {
    void first();
    void next();
    boolean isDone();
    Object currentItem();
}


/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:26:16
 * <p/>
 * note: 白盒聚集与外禀迭代子: 具体迭代子角色
 *
 *       由于迭代的逻辑是由 聚集对象本身提供的,所以这样的外禀迭代子往往仅仅保持迭代游标的位置
 */
public class ConcreteIterator implements Iterator {
    private ConcreteAggregate agg;
    private int index = 0;
    private int size = 0;

    public ConcreteIterator(ConcreteAggregate agg) {
        this.agg = agg;
        size = agg.size();
        index = 0 ;
    }

    public void first() {
        index = 0 ;
    }

    public void next() {
        if (index < size) {
            index++;
        }
    }

    public boolean isDone() {
        return (index >= size);
    }

    /**
     * 外禀迭代子通过调用白箱聚集提供的 接口
     * 实现迭代
     * @return
     */
    public Object currentItem() {
        return agg.getElement(index);
    }
}


/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:21:50
 * <p/>
 * note: 白盒聚集与外禀迭代子 客户端角色
 */
public class Client {
    private Iterator it;
    private Aggregate agg = new ConcreteAggregate();

    public static void main(String[] args) {
        Client client = new Client();
        client.operation();
    }

    public void operation() {
        it = agg.createIterator();
        while( !it.isDone() ) {
            System.out.println(it.currentItem().toString());
            it.next();
        }
    }
}

(2)  黑箱聚集和内禀迭代子

/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:51:39
 * <p/>
 * note: 黑箱聚集和内禀迭代子 : 迭代子角色
 */
public interface Iterator {
    void first();
    void next();
    boolean isDone();
    Object currentItem();

}



/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:52:19
 * <p/>
 * note: 黑箱聚集和内禀迭代子 : 聚集角色
 */
abstract public class Aggregate {

    public Iterator createIterator() {
        return null;
    }

}



/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:52:54
 * <p/>
 * note: 黑箱聚集和内禀迭代子 : 具体聚集角色
 *        这时候 聚集角色没有向 外界 提供 遍历方法
 */
public class ConcreteAggregate extends Aggregate {
    private Object[] objs = {"Monk Tang", "Monkey", "Pigsy", "Sandy", "Horse"};

    public Iterator createIterator() {
        return new ConcreteIterator();
    }

    /**
     * 具体迭代子角色是 具体聚集角色的内部类
     * 因此叫内禀迭代子
     */
    private class ConcreteIterator implements Iterator {
        private int currentIndex = 0;

        public void first() {
            currentIndex = 0;
        }

        public void next() {
            if (currentIndex < objs.length) {
                currentIndex++;
            }
        }

        public boolean isDone() {
            return (currentIndex == objs.length);
        }

        public Object currentItem() {
            return objs[currentIndex];
        }
    }
}



/**
 * User: liuwentao@wentao365.com
 * Date: 2008-12-9 Time: 14:52:04
 * <p/>
 * note: 黑箱聚集和内禀迭代子 : 客户角色
 */
public class Client {
    private Iterator it;
    private Aggregate agg = new ConcreteAggregate();

    public void operation() {
        it = agg.createIterator();
        while( !it.isDone() ) {
            System.out.println(it.currentItem().toString());
            it.next();
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.operation();
    }
}


如果一个聚集的接口没有提供修改聚集元素的方法,这个接口就是所谓的窄接口
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 遇到咬人的狗怎么办 自己的狗咬伤了怎么办 被蚂蚁咬了很痒怎么办 家里有蚂蚁怎么办能除根 被虫咬了红肿很痒怎么办 身体被虫子咬痒怎么办 手被乌龟咬破了怎么办 被乌龟咬出血了怎么办 被巴西乌龟咬了怎么办 孩子被乌龟咬了怎么办 如果被乌龟咬了怎么办 宝宝被乌龟咬了怎么办 上高中了偏文怎么办 上高中了很烦该怎么办 螳螂生完孩子后怎么办 小区门禁卡丢了怎么办 开门感应卡坏了怎么办 我的螃蟹生卵了怎么办 剑三账号被冻结怎么办 疤痕留下的红印怎么办 马桶刷子沾屎了怎么办 马桶上水管堵了怎么办 孕妇吃了姑娘果怎么办 出差被领导睡了怎么办 智融财富 跑路了怎么办 秒钱要是跑路了怎么办 新买的皮衣皱了怎么办 话说多了伤元气怎么办 话说多了嗓子哑怎么办 桂附地黄丸上火怎么办 乐视倒闭了会员怎么办 汽车上沾了水泥怎么办 汽车上的水泥点怎么办 小米4c启动不了怎么办 被骗取验证码后怎么办 电脑深度睡眠了唤醒不了怎么办 树脂镜片磨花了怎么办 详情页跳出率高怎么办 u盘拒绝访问怎么办win7 u盘洗了拒绝访问怎么办 u盘拒绝访问怎么办xp