`
DavyJones2010
  • 浏览: 154269 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

DesignPattern : Iterator

阅读更多

1. A simple mockup for ArrayList

package edu.xmu.designPattern.DesignPattern_Iterator;

public class ArrayList
{
	private Object[] objects = new Object[10];
	private int index = 0;

	public void add(Object o)
	{
		if (index == objects.length)
		{
			Object[] newObjects = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = obj;
		index++;
	}

	public int size()
	{
		return index;
	}
}

 

package edu.xmu.designPattern.DesignPattern_Iterator;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class ArrayListTest
{
	@Test
	public void test()
	{
		ArrayList list = new ArrayList();

		for (int i = 0; i < 15; i++)
		{
			list.add(new Object());
		}
		assertEquals(15, list.size());
	}
}

    Comments: The benefits of using ArrayList instead of using raw list is that we don't have to worry about the size of the list. It will expand automatically.

 

 

2. A simple mockup for LinkedList

package edu.xmu.designPattern.DesignPattern_Iterator;

public class Node
{
	private Object data;
	private Node next;

	public Node()
	{
		super();
	}

	public Node(Object currentObject, Node next)
	{
		super();
		this.data = currentObject;
		this.next = next;
	}

	public void add(Node node)
	{
		if (null == next)
		{
			next = node;
		} else
		{
			next.add(node);
		}
	}

	public Object getData()
	{
		return data;
	}

	public void setData(Object data)
	{
		this.data = data;
	}

	public Node getNext()
	{
		return next;
	}

	public void setNext(Node next)
	{
		this.next = next;
	}

}

 

package edu.xmu.designPattern.DesignPattern_Iterator;

public class LinkedList
{
	private Node header = null;
	private int size = 0;

	public void add(Object obj)
	{
		if (null == header)
		{
			header = new Node(obj, null);
		} else
		{
			header.add(new Node(obj, null));
		}
		size++;
	}

	public int size()
	{
		return size;
	}
}

 

package edu.xmu.designPattern.DesignPattern_Iterator;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class LinkedListTest
{
	@Test
	public void test()
	{
		LinkedList list = new LinkedList();

		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}
		assertEquals(15, list.size());
	}
}

   Comments: This is a simple mockup for one-directional LinkedList.

 

 

3. Take container's replaceability into consideration.

    1) How can we switch the implementation of List from ArrayList to LinkedList without changing code of using them?

        Put it another way, how can we reduce the coupling between List and code that using this List?

        We can use Interface. And let ArrayList and LinkedList implements this Interface.

package edu.xmu.designPattern.DesignPattern_Iterator;

public interface Collection
{
	public void add(Object obj);
	public int size();
}

 

package edu.xmu.designPattern.DesignPattern_Iterator;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class LinkedListTest
{
	@Test
	public void test()
	{
		// Collection list = new ArrayList();
		Collection list = new LinkedList();
		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}
		assertEquals(15, list.size());
	}
}

   Comments: Interface Oriented Programming.

 

 

4. Here is another important part of List fuction: Traversal --> This fuction is used quite often.

    How can we get every single Object from this List?

    1) The way we traverse an ArrayList:

package edu.xmu.designPattern.DesignPattern_Iterator;

import org.junit.Test;

public class ArrayListTest
{
	@Test
	public void test()
	{
		ArrayList list = new ArrayList();

		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}
		
		for(int i = 0; i < list.size(); i++)
		{
			Cat cat = list.get(i);
		}
	}
}

 

   2) The way we traverse a LinkedList:

package edu.xmu.designPattern.DesignPattern_Iterator;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class LinkedListTest
{
	@Test
	public void test()
	{
		LinkedList list = new LinkedList();
		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}
		
		Node currentNode = list.getHeader();
		while (null != currentNode)
		{
			Cat cat = (Cat) currentNode.getData();
			currentNode = currentNode.next();
		}
	}
}

   Comments:

        1)  Q: Why can't we define this in interface, like get(int index)?

             A: It's ok when using get(int index) for ArrayList because it's list inside which is convenient for random access.

                But when it comes to LinkedList when traversing using get(int index), it would have to find the element from head to tail.

                The time cost for traversal in ArrayList is O(n). The time cost for traversal in LinkedList is O(n^2) = (1 + 2 + 3 + 4 + ... + n);

                So we cannot using this to traverse all the list.

            E: And it may also possible for us to implements this Interface using the relization of Binary-Tree or some other techniques. Should we expose preorder/inorder/postorder traversal to the client?

       2)  Q: So, how can we figure out an easy way which have not only reduced the coupling between Implementation and Usage but also reduced the time cost for LinkedList?

            A: Think about Iterator Design Pattern as a solution.

       3) Now the problem is that different implementation may have different ways of traversal. How can we define a unified way to traversal them?

 

 5. A simple mockup for ArrayList Iterator

    1) Iterator interface

package edu.xmu.designPattern.DesignPattern_Iterator;

public interface Iterator
{
	public Object next();

	public boolean hasNext();
}

   2) ArrayList implementation

package edu.xmu.designPattern.DesignPattern_Iterator;

public class ArrayList implements Collection
{
	private Object[] objects = new Object[10];
	private int index = 0;

	public void add(Object obj)
	{
		if (index == objects.length)
		{
			Object[] newObjects = new Object[objects.length * 2];
			System.arraycopy(objects, 0, newObjects, 0, objects.length);
			objects = newObjects;
		}
		objects[index] = obj;
		index++;
	}

	public int size()
	{
		return index;
	}

	public Iterator iterator()
	{
		return new ArrayListIterator();
	}

	private class ArrayListIterator implements Iterator
	{
		private int currentIndex = 0;

		public Object next()
		{
			Object currentObject = objects[currentIndex];
			currentIndex++;
			return currentObject;
		}

		public boolean hasNext()
		{
			if (currentIndex > index || null == objects[currentIndex])
			{
				System.out.println("no next");
				return false;
			} else
			{
				System.out.println("has next");
				return true;
			}
		}
	}
}

   3) ArrayList test case

package edu.xmu.designPattern.DesignPattern_Iterator;

import org.junit.Test;

public class ArrayListTest
{
	@Test
	public void test()
	{
		Collection list = new ArrayList();

		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}
		for (Iterator iter = list.iterator(); iter.hasNext();)
		{
			Cat cat = (Cat) iter.next();
			System.out.println(cat);
		}
	}
}

   4) Output is correct.

 

 6. A simple mockup for LinkedList Iterator

    1) The implementation for LinkedList Iterator

package edu.xmu.designPattern.DesignPattern_Iterator;

public class LinkedList implements Collection
{
	private Node header = null;
	private int size = 0;

	public void add(Object obj)
	{
		if (null == header)
		{
			header = new Node(obj, null);
		} else
		{
			header.add(new Node(obj, null));
		}
		size++;
	}

	public int size()
	{
		return size;
	}

	public Iterator iterator()
	{
		return new LinkedListIterator();
	}

	private class LinkedListIterator implements Iterator
	{
		private Node currentNode = header;

		public Object next()
		{
			if (currentNode == header)
			{
				Node temp = header;
				currentNode = header.getNext();
				return temp.getData();
			} else
			{
				Node temp = currentNode.getNext();
				currentNode = temp;
				return temp.getData();
			}
		}

		public boolean hasNext()
		{
			if (null != currentNode.getNext())
			{
				return true;
			} else
			{
				return false;
			}
		}
	}
}

   2) The test case

package edu.xmu.designPattern.DesignPattern_Iterator;

import org.junit.Test;

public class LinkedListTest
{
	@Test
	public void test()
	{
		LinkedList list = new LinkedList();
		for (int i = 0; i < 15; i++)
		{
			list.add(new Cat(i, "Cat" + i));
		}

		for (Iterator iter = list.iterator(); iter.hasNext();)
		{
			System.out.println((Cat) iter.next());
		}
	}
}

   3) The output is correct.

分享到:
评论

相关推荐

    DesignPattern::pencil:设计模式_java实现以及详解

    本资源“DesignPattern::pencil:设计模式_java实现以及详解”提供了一套详细的学习材料,帮助开发者理解和应用设计模式。 该资源的作者是“养码青年-Style”,他通过这个项目记录了自己的设计模式学习过程。鼓励...

    DesignPattern:C#设计模式示例

    "DesignPattern:C#设计模式示例"这个资源很可能是包含多个C#实现的设计模式示例代码库。 设计模式通常分为三类:创建型、结构型和行为型。每种模式都解决了特定场景下的问题,并提供了良好的代码组织和扩展性。 ...

    DesignPattern:设计模式

    DesignPattern-master这个压缩包可能包含了一个关于设计模式的项目或者教程资源。 设计模式分为三类:创建型模式(Creational Patterns)、结构型模式(Structural Patterns)和行为型模式(Behavioral Patterns)...

    designPattern:设计模式相关代码实现

    "designPattern:设计模式相关代码实现"这个项目,显然提供了不同设计模式在Java语言中的实际应用示例。 在Java世界里,设计模式主要分为三大类:创建型模式、结构型模式和行为型模式。每种模式都针对特定的编程问题...

    DesignPattern:设计模式演示程序

    这个名为"DesignPattern"的压缩包文件很可能包含了一个Java实现的各种设计模式的示例程序。 在这个"DesignPattern-master"目录中,我们可以期待找到一系列与设计模式相关的Java源代码文件(.java),每个文件或...

    阅读java源码-JavaDesignPattern:23种设计模式Java实现

    在“阅读java源码-JavaDesignPattern:23种设计模式Java实现”中,我们将深入探讨这23种设计模式的Java实现。 1. **创建型模式**(Creational Patterns): - **单例模式(Singleton)**:确保一个类只有一个实例,...

    Java-DesignPattern:Java中23种常见的设计模式

    本资料包“Java-DesignPattern:Java中23种常见的设计模式”涵盖了软件设计中的重要概念,以下是这些模式的详细说明: 1. **单例模式(Singleton)**:确保一个类只有一个实例,并提供全局访问点。防止其他对象多次...

    Design Pattern英文版

    设计模式(Design Pattern)是软件工程中的一种经验总结,它是在特定上下文中为解决常见问题而提出的一套可复用的解决方案。设计模式并不直接实现为代码,而是提供了一种在面向对象设计中如何处理常见问题的指南。...

    designpattern:PHP设计模式教程

    本教程的"designpattern-master"可能包含以上各种设计模式的实例代码和详细解释,帮助学习者通过实践掌握这些模式。在实际项目中,灵活运用这些设计模式可以提高代码质量,使项目更加健壮和易于维护。对于PHP开发者...

    DesignPattern:Head First Design Patterns 源代码 Swift 版

    在`DesignPattern-master`这个压缩包中,你可能会看到以上提到的各种设计模式的Swift实现。通过阅读和分析这些源码,你可以更深入地了解如何在实际项目中应用这些模式,以及它们在Swift语法和特性下的具体表现。同时...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    DesignPattern:设计模式的学习笔记和示例代码

    设计模式是软件工程中的一种最佳实践,它是在特定情境下解决常见问题的经验总结。...在DesignPattern-master这个压缩包中,你可以找到关于这些模式的详细讲解和实例代码,为你的Java开发之旅提供宝贵的参考资料。

    DesignPattern:Java中的一些设计模式实现

    Java集合框架中的Iterator接口就是迭代器模式的体现。 5. 状态模式:允许一个对象在其内部状态改变时改变其行为。对象看起来似乎修改了它的类。在Java中,状态模式常用于处理具有多种状态的对象。 6. 观察者模式:...

    JAVA software design pattern

    14. **迭代器模式(Iterator)**:提供一种方法顺序访问聚合对象中的元素,而又不暴露其底层表示。 15. **责任链模式(Chain of Responsibility)**:避免将处理逻辑硬编码到对象中,让对象自行决定是否处理请求,...

    DesignPattern:在这里写下 23 个 DesignPatterns,我可以分享最好的来解释它是什么

    设计模式是软件工程中经过长期实践验证的、解决常见问题的有效解决方案模板,它们代表了在特定上下文中,针对某...在`DesignPattern-master`这个压缩包中,可能包含了这些设计模式的Java实现示例,供学习者参考和实践。

    Head First Design Pattern

    3. 行为型设计模式:例如策略模式(Strategy)、模板方法模式(Template Method)、观察者模式(Observer)、命令模式(Command)、迭代器模式(Iterator)、访问者模式(Visitor)、备忘录模式(Memento)、状态...

    designpattern.zip

    本资源"designpattern.zip"包含了对Java中23种经典设计模式的详细讲解和代码实例,对于中高级Java工程师来说,是提升开发技能的必备学习材料。 设计模式通常分为创建型、结构型和行为型三大类。创建型设计模式关注...

    design patterns elements of reusable object-oriented software

    ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...

Global site tag (gtag.js) - Google Analytics