zoukankan      html  css  js  c++  java
  • 设计模式迭代器模式

    类图

     

    源码

    AbstractIterator.java 抽象迭代器
    package com.iterator;
    
    
    public abstract class AbstractIterator {
        //抽象迭代器类
        //移到下一个元素
        public abstract void next();
        
        //移到上一个元素
        public abstract void previous();
        
        //获取上一个元素返回object
        public abstract Object getPrevious();
        
        //获取下一个元素放回OBJECT
        public abstract Object getNext();
        //是否是第一个元素boolean
        public abstract boolean isNext();
        //是否是最后一个元素
        public abstract boolean isFrist();
        
    }
    
    ProducteIterator.java
    package com.iterator;
    
    import java.util.ArrayList;
    import java.util.List;
     
    public class ProducteIterator extends AbstractIterator {
        private List<Object> objectList = new ArrayList<Object>();
        private int cursor1;    //游标
        private int cursor2;    //游标2
        
        public ProducteIterator(ConcreteObjectList obj) {
            // TODO Auto-generated constructor stub
            this.objectList=obj.getObjectList();
            cursor1=0;
            cursor2=objectList.size()-1;
        }
        @Override
        public void next() {
            // TODO Auto-generated method stub
            if(cursor1<objectList.size()) {
                cursor1++;
            }
        }
     
        @Override
        public void previous() {
            // TODO Auto-generated method stub
            if(cursor2>-1) {
                cursor2--;
            }
        }
     
        @Override
        public Object getPrevious() {
            // TODO Auto-generated method stub
            return objectList.get(cursor2);
        }
     
        @Override
        public Object getNext() {
            // TODO Auto-generated method stub
            return objectList.get(cursor1);
        }
     
        @Override
        public boolean isNext() {
            // TODO Auto-generated method stub
            return (cursor1==objectList.size());
        }
     
        @Override
        public boolean isFrist() {
            // TODO Auto-generated method stub
            return (cursor2==-1);
        }
     
    }
    
    AbstractObjectList.java    抽象聚合对象
    
    package com.iterator;
    
    import java.util.*;
     
    public abstract class AbstractObjectList {
        //创建一个object 的arraylist集合
        protected List<Object> objectList = new ArrayList<Object>();
        public AbstractObjectList(List<Object> list) {
            // TODO Auto-generated constructor stub
            this.objectList=list;
        }
        //添加方法。
        public void AddObject(Object o) {
            this.objectList.add(o);
        }
        //删除方法。
        public void RemoveObject(Object o) {
            this.objectList.remove(o);
        }
        //获取到list
        public List<Object> getObjectList(){
            return objectList;
        }
        //获取arrayList
        //获取迭代器的工厂方法
        public abstract AbstractIterator createIterator();
     
    }
    
    ConcreteObjectList.java 具体聚合对象
    package com.iterator;
    
    import java.util.*;
     
    public abstract class AbstractObjectList {
        //创建一个object 的arraylist集合
        protected List<Object> objectList = new ArrayList<Object>();
        public AbstractObjectList(List<Object> list) {
            // TODO Auto-generated constructor stub
            this.objectList=list;
        }
        //添加方法。
        public void AddObject(Object o) {
            this.objectList.add(o);
        }
        //删除方法。
        public void RemoveObject(Object o) {
            this.objectList.remove(o);
        }
        //获取到list
        public List<Object> getObjectList(){
            return objectList;
        }
        //获取arrayList
        //获取迭代器的工厂方法
        public abstract AbstractIterator createIterator();
     
    }
    
    Client.java 测试类
    
    package com.iterator;
    import java.util.ArrayList;
    import java.util.List;
     
    public class Client {
        public static void main(String[] args) {
            List<Object> list1 =new ArrayList<Object>();
            AbstractObjectList list = new ConcreteObjectList(list1);
            list.AddObject("20193901");
            list.AddObject("20193902");
            list.AddObject("20193903");
            list.AddObject("20193904");
            list.AddObject("20193905");
            list.AddObject("20193906");
            list.AddObject("20193907");
            list.AddObject("20193908");
            list.AddObject("20193909");
            list.AddObject("20193910");
            list.AddObject("20193911");
            list.AddObject("20193912");
            
            AbstractIterator iterator =list.createIterator();
            System.out.println("正向遍历");
            
            while(!iterator.isNext()) {
                System.out.print(iterator.getNext()+"\n");
                iterator.next();
            }
            
            //反向遍历
            System.out.println("反向遍历");
            while(!iterator.isFrist()) {
                System.out.print(iterator.getPrevious()+"\n");
                iterator.previous();
            }
        }
    }

    测试截图

     

  • 相关阅读:
    PAT-A 1004. Counting Leaves
    PAT-A 1094. The Largest Generation
    图的邻接表表示及其BFS遍历
    图的邻接表表示及其遍历
    PAT-A 1024. Palindromic Number
    PAT-A 1019. General Palindromic Number
    PAT-B 1007.素数对猜想
    PAT-A 1048. Find Coins
    PAT-A 1037. Magic Coupon
    PAT-A 1099.Build A Binary Search Tree
  • 原文地址:https://www.cnblogs.com/lx06/p/15688547.html
Copyright © 2011-2022 走看看