Archive

Archive for March, 2016

Iterator Pattern

March 22, 2016 Leave a comment

Iterator pattern is used to provide a way to access the elements of an aggregate object sequentially without exposing its internal structure.

Iterator Pattern

Iterator Pattern

Here is example.

SubjectType.java

public enum SubjectType {
     MANDATORY, OPTIONAL, ALL
}

Subject.java

 public class Subject {

    private String name;
    private String lecturer;
    private SubjectType type;
    
    public Subject(){
        
    }
    public Subject(String name, String lecturer, SubjectType type) {
        super();
        this.name = name;
        this.lecturer = lecturer;
        this.type = type;
    }
    public String getName() {
        return name;
    }
    public String getLecturer() {
        return lecturer;
    }
    public SubjectType getType() {
        return type;
    }
    @Override
    public String toString() {
        return "Subject [name=" + name + ", lecturer=" + lecturer + ", type=" + type + "]";
    }

}
 

SubjectCollection.java

public interface SubjectCollection {
    public void addSubject(Subject subject);
    public void removeSubject(Subject subjet);
    public SubjectIterator iterator(SubjectType type);
}

SubjectIterator.java

 public interface SubjectIterator {
   public Boolean hasNext();
   public Subject next();
   public void remove();
} 

SubjectCollectionImpl.java

public class SubjectCollectionImpl implements SubjectCollection {

    private List<Subject> subjects;

    public SubjectCollectionImpl() {
        subjects = new ArrayList<Subject>();
    }

    public void addSubject(Subject subject) {
        subjects.add(subject);
    }

    public void removeSubject(Subject subject) {
        subjects.remove(subject);
    }

    public SubjectIterator iterator(SubjectType type) {
        return new Iterator(type, subjects);
    }

    private class Iterator implements SubjectIterator {

        private SubjectType type;
        private int index;
        private List<Subject> subjects;

        private Iterator(SubjectType type, List<Subject> subjects) {
            this.type = type;
            this.subjects = subjects;
        }

        public Boolean hasNext() {
            while (index < subjects.size()) {
                Subject s = subjects.get(index);
                if (s.getType().equals(type)) {
                    return true;
                } else {
                    index++;
                }
            }
            return false;
        }

        public Subject next() {
            Subject s = subjects.get(index);
            index++;
            return s;
        }

        public void remove() {
            subjects.remove(index);
            index++;
        }

    }
}

App.java

public class App 
{
    public static void main( String[] args )
    {
    	SubjectCollection subjectCollection = populateSubject();
    	SubjectIterator iterator = subjectCollection.iterator(SubjectType.ALL);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		System.out.println(s.toString());
    	}
    	System.out.println("-----------------------");
    	iterator = subjectCollection.iterator(SubjectType.MANDATORY);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		System.out.println(s.toString());
    	}
    	System.out.println("-----------------------");
    	iterator = subjectCollection.iterator(SubjectType.OPTIONAL);
    	while(iterator.hasNext()){
    		Subject s = iterator.next();
    		iterator.remove();
    		System.out.println(s.toString());
    	}
    }
    
    public static SubjectCollection populateSubject(){
    	SubjectCollection subjectCollection = new SubjectCollectionImpl();
    	
    	subjectCollection.addSubject((new Subject("Accounting", "John", SubjectType.OPTIONAL)));
    	subjectCollection.addSubject((new Subject("Art", "Smith", SubjectType.OPTIONAL)));
    	subjectCollection.addSubject((new Subject("Chemistry", "Lennon", SubjectType.ALL)));
    	subjectCollection.addSubject((new Subject("Civil", "Alan", SubjectType.ALL)));
    	subjectCollection.addSubject((new Subject("Economics", "Mike", SubjectType.MANDATORY)));
    	subjectCollection.addSubject((new Subject("Education", "Joe", SubjectType.MANDATORY)));
    	
    	return subjectCollection;
    }
}

Result.

Iterator Result

Iterator Result

Advertisements
Categories: Java Tags: , ,