7 Replies - 2155 Views - Last Post: 19 February 2012 - 05:52 PM Rate Topic: -----

#1 raihan26  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:04 PM

I am new to design patterns. And I was trying to implement Iterator Design Patter in Java in Linked list to display the linked list from front.

// Creating a doubly linked list.

        class DoublyLinked {
    	doubleLinkedList = new DoubleLinkedList();

// This insert into Doubly Linked List
	doubleLinkedList.insert("john")
        doubleLinkedList.insert("ssss")
        doubleLinkedList.insert("ttt")

//After insertion, I want ti implement Iteratot Design Pattern to Display the linked list.
//So how can we achieve this- Any kind of suggestions will be appreciated.


         }


    class DoubleLinkedList {
    
    	private NewLink firstNode;
    	private NewLink lastNode;
    	private NewLink rootNode;
    

    	public DoubleLinkedList() {

	firstNode = null;
    	lastNode  = null;
    
    	}
    
    }



    class NewLink {
    
    	public String  data;
    	public NewLink nextPointer;
    	public NewLink previousPointer;
    
    	public NewLink(String id) {
    
    		data = id;
    
    	}

    	public String toString() {
    
    		return "{" + data + "} ";
    
    	}
    }




Is This A Good Question/Topic? 0
  • +

Replies To: Implementation of Iterator Design Pattern in Java

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:19 PM

At the very least, you should create an insert method first, so that you can test your iterator.
Was This Post Helpful? 0
  • +
  • -

#3 raihan26  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 47
  • Joined: 04-February 10

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:23 PM

Insert method is already created and its working fine. I haven't posted it. I just need to work on iterator design pattern so that I can iterate.
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:31 PM

1. Have your list implement Iterable.
2. Have the iterator method return an implementation of Iterator for your list.

That's basically it.

import java.util.Iterator;

class MyList<E> implements Iterable<E> {

	public Iterator<E> iterator() {
		return new MyListIterator();
	}
	
	private class MyListIterator implements Iterator<E> {

		@Override
		public boolean hasNext() {
			return false;
		}

		@Override
		public E next() {
			return null;
		}

		@Override
		public void remove() {
		}
		
	}
}


This post has been edited by blackcompe: 19 February 2012 - 05:51 PM
Reason for edit:: Updated code.

Was This Post Helpful? 0
  • +
  • -

#5 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:37 PM

Personally I wouldnt have my Iterator declare its own type if it is not implementing its own methods
 private class MyListIterator<V> implements Iterator<V> 


to
 private class MyListIterator implements Iterator<V> 


This post has been edited by ianian112: 19 February 2012 - 05:39 PM

Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:41 PM

Quote

Personally your Iterator should not have a type declared with it unless you plan to implement your own methods


Can you clarify? I don't quite understanding what you're saying. Removing the formal type parameter breaks my code.

This post has been edited by blackcompe: 19 February 2012 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:44 PM

View Postblackcompe, on 19 February 2012 - 05:41 PM, said:

Quote

Personally your Iterator should not have a type declared with it unless you plan to implement your own methods


Can you clarify? I don't quite understanding what you're saying.

Since all the methods in your iterator class come from the interface Iterator, they will all use the generic type that the iterator has
public class Iter<E> implments Iterator<Z>


the next method will return an object of type Z. This means that if you are only overriding the methods in Iterator, there is no need for a generic type on Iter

This post has been edited by ianian112: 19 February 2012 - 05:46 PM

Was This Post Helpful? 1
  • +
  • -

#8 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Implementation of Iterator Design Pattern in Java

Posted 19 February 2012 - 05:52 PM

Quote

the next method will return an object of type Z. This means that if you are only overriding the methods in Iterator, there is no need for a generic type on Iter


I see. Glad you caught that. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1