Design Patterns _ iteration mode

     Why use an iterative mode? Compared to such an array of simple data containers, sometimes we need to traverse, operate some of the more complex data container, such as ArrayList, HashMap and so on. So when traversing or operating these data container, you may encounter such a situation:

    An iterative manner has not changed, the changed data container.

    Two data container is not changed, but the iterative logic has changed.

    The problem is, we do not want to change the client code, then it is only possible to change the content of abstraction, allowing the client to operate abstract interface. This is the "on-off" principles mentioned, some of these changes may occur in the project need to be encapsulated. In this case, the client need not know the particular set is manipulated what type, when the need to change an iterative way, which requires the introduction of a new iteration can be sub-object.

    There is also a wide and a narrow interface concept interface. Wide interface is only set its open interface methods modified object elements, as opposed to not open the narrow interface is modified. You may say a little abstract, to enumerate here some role in it:

    Abstract iterator (Iterator), defines the methods required to traverse a set of objects

    DETAILED iterator (ConcreteIterator), abstract iterators implement, and maintain the current cursor position.

    Abstract collections (or Aggregate Collection), given interface methods to create iterators, such as the Java Collection interface iterator ()

    DETAILED set (such as the ArrayList), implements a method to obtain specific iterator, attention here to give particular iterators are provided so that this iterator can invoke a method of operating a set of elements (add / delete), which is above said wide interface .

    Client: iterator owns the collection and reference may be iterative iterators to traverse the interface element, the collection element may be operated by the iterator.

/**
 * 抽象迭代器接口
 * @author wly
 *
 */
public interface AbstractIterator {

	boolean hasNext();
	Object next();
	void addItem(Object o); //操作数据容器方法 
}
 
/**
 * 抽象数据容器接口
 * @author wly
 *
 */
public interface AbstractCollection {
	
	int size();
	void add(Object o);
	
	AbstractIterator iterator(); //得到迭代器引用
}
 
package Iterator;

public class ConcreteCollection implements AbstractCollection {

	int INIT_LENGTH = 10; //容器初始容量
	Object[] objects = new Object[10];
	int filledNum = 0; //表示当前容器填充量
	@Override
	public int size() {
		return filledNum;
	}

	@Override
	public void add(Object o) {
		if(!(size() < objects.length)) {
			Object[] objects2 = new Object[objects.length + INIT_LENGTH];
			System.arraycopy(objects, 0, objects2, 0, filledNum);
		}
		objects[filledNum + 1] = o;
		filledNum ++;
	}
	
	@Override
	public AbstractIterator iterator() {
		
		return new ConcreteIterateA();
	}
	
	/**
	 * 具体迭代器类一
	 */
	public class ConcreteIterateA implements AbstractIterator {

		int cursor = 0;
		@Override
		public boolean hasNext() {
			if(!(cursor >= size())) {
				return true;
			} else {
				return false;
			}
		}

		/**
		 * 操作容器数据
		 */
		public void addItem(Object o) {
			if(o instanceof String) {
				String s = (String)o;
				add("A__" + o.toString());
			}
		}
		
		@Override
		public Object next() {
			cursor ++;
			return objects[cursor];
		}
	}
	

	/**
	 * 具体迭代器类二,
	 * 实现了Iterator接口并保持迭代过程中的游标位置,使用内部类的形式是具体集合类对具体迭代子对象开放的
	 * @author wly
	 *
	 */
	public class ConcreteIterateB implements AbstractIterator {

		int cursor = 0;

		@Override
		public boolean hasNext() {
			if(!(cursor >= size())) {
				return true;
			} else {
				return false;
			}
		}
		
		/**
		 * 操作容器数据
		 */
		public void addItem(Object o) {
			if(o instanceof String) {
				String s = (String)o;
				add("B__" + o.toString());
			}
		}
		
		@Override
		public Object next() {
			cursor ++;
			return objects[cursor];
		}

	}
}
 
/**
 * 客户端
 * @author wly
 *
 */
public class Client {

	public static void main(String[] args) {
		
		ConcreteCollection collection = new ConcreteCollection();
		AbstractIterator iterator = (AbstractIterator)collection.iterator();
		iterator.addItem("A");
		iterator.addItem("B");
		iterator.addItem("C");
		while(iterator.hasNext()) {
			System.out.println(iterator.next().toString());
		}
	}
}

 输出:A__A
          A__B
          A__C

When the return value into a new ConcreteIterateB () () in ConcreteCollection the iterator, output:

          B__A
          B__B
          B__C

Similarly, we can also replace the iterator traversal methods at any time.

Reproduced in: https: //my.oschina.net/cjkall/blog/195828

Guess you like

Origin blog.csdn.net/weixin_34331102/article/details/91756145