美文网首页
设计模式-迭代器设计模式

设计模式-迭代器设计模式

作者: woochen123 | 来源:发表于2017-11-23 11:32 被阅读0次

    1.定义

    提供一种方法顺序访问某个容器的元素,但是又不暴露容器内部的数据显示

    2.示例

    public interface Iterator<T> {
        T next();
        boolean hasNext();
    }
    
    /**
     *qq迭代器
     *@author woochen123
     *@time 2017/11/23 9:20
     *@desc
     */
    public class QQIterator implements Iterator<UserBean> {
        List<UserBean> list;
        int index;
    
        public QQIterator(List<UserBean> list) {
            this.list = list;
        }
    
        @Override
        public UserBean next() {
            return list.get(index++);
        }
    
        @Override
        public boolean hasNext() {
            return index < list.size();
        }
    }
    
    /**
     *微信迭代器
     *@author woochen123
     *@time 2017/11/23 9:19
     *@desc
     */
    public class WxIterator implements Iterator<UserBean> {
        UserBean[] array;
        int index;
    
        public WxIterator(UserBean[] array) {
            this.array = array;
        }
    
        @Override
        public UserBean next() {
            return array[index++];
        }
    
        @Override
        public boolean hasNext() {
            return index < array.length;
        }
    
    }
    
    public interface Aggregate<T> {
        /**
         * 公共接口
         * @return 返回迭代器
         */
        Iterator<T> interator();
    }
    
    /**
     * qq登录系统
     * @author chenwuchao
     * @time 2017/11/22 17:14
     * @desc
     */
    public class QQUserSystem implements Aggregate<UserBean>{
        List<UserBean> list;
    
        public QQUserSystem() {
            list = new ArrayList<>();
            list.add(new UserBean("小明", "12"));
            list.add(new UserBean("小红", "13"));
            list.add(new UserBean("小刚", "14"));
        }
    
        public List<UserBean> elements() {
            return list;
        }
    
        @Override
        public Iterator<UserBean> interator() {
            return new QQIterator(list);
        }
    
    }
    
    /**
     *微信登录系统
     *@author woochen123
     *@time 2017/11/23 9:23
     *@desc
     */
    public class WxUserSystem implements Aggregate<UserBean>{
        private UserBean[] array ;
    
        public WxUserSystem() {
            array = new UserBean[3];
            array[0] = new UserBean("小花","12");
            array[1] = new UserBean("小华","13");
            array[2] = new UserBean("小方","14");
        }
    
        public UserBean[] elements() {
            return array;
        }
    
        @Override
        public Iterator<UserBean> interator() {
            return new WxIterator(array);
        }
    }
    
    public class Client {
        private QQUserSystem qqUserSystem;
        private WxUserSystem wxUserSystem;
    
        public void main(){
            UserBean userBean = new UserBean("woochen123","123456");
            qqUserSystem = new QQUserSystem();
            wxUserSystem = new WxUserSystem();
            //qq系统中查询
            UserBean userInfo = query(userBean, qqUserSystem.interator());
            //从wx系统中查询
            if(userInfo == null){
                userInfo = query(userBean,wxUserSystem.interator());
            }
        }
    
        private UserBean query(UserBean userBean, Iterator<UserBean> interator) {
            while (interator.hasNext()) {
                UserBean element = interator.next();
                if (element.getName().equals(userBean.getName()) && element.getPwd().equals(userBean.getPwd())) {
                    return element;
                }
            }
            return null;
        }
    
    }
    

    相关文章

      网友评论

          本文标题:设计模式-迭代器设计模式

          本文链接:https://www.haomeiwen.com/subject/xqzwvxtx.html