Skip to content

Commit e756ae1

Browse files
committed
Iterator design pattern - BONUS EXAMPLE -
Multiple iterators to encapsulate and hide filter logic/algorithm
1 parent e96236f commit e756ae1

6 files changed

Lines changed: 225 additions & 13 deletions

File tree

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.premaseem.iterator;
2+
3+
import java.util.Iterator;
4+
import java.util.Map.Entry;
5+
6+
public class ClientForEmployeeIterator {
7+
8+
public static void main(String[] args) {
9+
EmployeeManger employeeManager = new EmployeeManger();
10+
11+
System.out
12+
.println("This is Iterator Pattern example where you can list all the male / female emplyee list using iterators. The data structure details are abstraced behind the iterator interface ");
13+
14+
employeeManager.addEmployee(new Employee("aseem1", "M"));
15+
employeeManager.addEmployee(new Employee("Meera1", "f"));
16+
employeeManager.addEmployee(new Employee("aseem2", "M"));
17+
employeeManager.addEmployee(new Employee("sony1", "Mf"));
18+
employeeManager.addEmployee(new Employee("Meera2", "f"));
19+
employeeManager.addEmployee(new Employee("Meera3", "f"));
20+
employeeManager.addEmployee(new Employee("Meeralast", "f"));
21+
employeeManager.addEmployee(new Employee("sony2", "fM"));
22+
employeeManager.addEmployee(new Employee("aseem1", "m"));
23+
employeeManager.addEmployee(new Employee("aseemlast", "m"));
24+
25+
employeeManager.employeeMap.put(1,new Employee("aseem1", "M"));
26+
employeeManager.employeeMap.put(2,new Employee("aseem2", "F"));
27+
employeeManager.employeeMap.put(3,new Employee("aseem3", "M"));
28+
employeeManager.employeeMap.put(1,new Employee("aseem4", "F"));
29+
30+
MaleEmplyeeIterator maleEmployeeIterator = employeeManager.getMaleEmployeeIterator();
31+
printEmployeeList(maleEmployeeIterator);
32+
33+
FeMaleEmplyeeIterator feMaleEmployeeIterator = employeeManager.getFeMaleEmployeeIterator();
34+
printEmployeeList(feMaleEmployeeIterator);
35+
36+
Iterator<Employee> allEmployeeIterator = employeeManager.getAllEmployeeIterator();
37+
printEmployeeList(allEmployeeIterator);
38+
39+
printEmplyeeMap(employeeManager.printEmployeeMap());
40+
41+
}
42+
43+
private static void printEmplyeeMap(Iterator<Entry<Integer, Employee>> iterator) {
44+
System.out.println("Printing employee map ");
45+
while (iterator.hasNext()) {
46+
Entry<Integer, Employee> next = iterator.next();
47+
System.out.println(next);
48+
}
49+
}
50+
51+
private static void printEmployeeList(Iterator iterator) {
52+
System.out.println("Printing the list for " + iterator.getClass().getSimpleName());
53+
while (iterator.hasNext()) {
54+
System.out.println(iterator.next());
55+
}
56+
}
57+
58+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.premaseem.iterator;
2+
3+
public class Employee {
4+
5+
String name;
6+
String sex;
7+
8+
public Employee(String name, String sex){
9+
this.name = name;
10+
this.sex = sex;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public String getSex() {
18+
return sex;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return sex + " " + name ;
24+
}
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.premaseem.iterator;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.Iterator;
6+
import java.util.List;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
10+
public class EmployeeManger {
11+
12+
List<Employee> emplyeelist = new ArrayList<Employee>();
13+
Map<Integer, Employee> employeeMap = new HashMap<Integer, Employee>();
14+
15+
void addEmployee(Employee emp) {
16+
emplyeelist.add(emp);
17+
}
18+
19+
boolean removeEmployee(Employee emp) {
20+
return emplyeelist.remove(emp);
21+
}
22+
23+
MaleEmplyeeIterator getMaleEmployeeIterator() {
24+
return new MaleEmplyeeIterator(emplyeelist);
25+
}
26+
27+
FeMaleEmplyeeIterator getFeMaleEmployeeIterator() {
28+
return new FeMaleEmplyeeIterator(emplyeelist);
29+
}
30+
31+
Iterator<Employee> getAllEmployeeIterator() {
32+
return emplyeelist.iterator();
33+
}
34+
35+
public Iterator<Entry<Integer, Employee>> printEmployeeMap() {
36+
return employeeMap.entrySet().iterator();
37+
}
38+
39+
void calculateEmpSalary() {
40+
41+
}
42+
43+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.premaseem.iterator;
2+
3+
import java.util.Iterator;
4+
import java.util.List;
5+
6+
public class MaleEmplyeeIterator implements Iterator<Employee> {
7+
8+
private List<Employee> emplyeelist;
9+
private int position;
10+
public MaleEmplyeeIterator(List<Employee> emplyeelist){
11+
this.emplyeelist = emplyeelist;
12+
}
13+
14+
@Override
15+
public boolean hasNext() {
16+
for ( ;position < emplyeelist.size(); position++) {
17+
if ("m".equalsIgnoreCase ((emplyeelist.get(position)).getSex())) {
18+
return true;
19+
}
20+
}
21+
return false;
22+
}
23+
24+
@Override
25+
public Employee next() {
26+
Employee employee = emplyeelist.get(position);
27+
position++;
28+
return employee;
29+
}
30+
31+
@Override
32+
public void remove() {
33+
emplyeelist.iterator().remove();
34+
}
35+
}
36+
37+
class FeMaleEmplyeeIterator implements Iterator<Employee> {
38+
39+
private List<Employee> emplyeelist;
40+
private int position;
41+
public FeMaleEmplyeeIterator(List<Employee> emplyeelist){
42+
this.emplyeelist = emplyeelist;
43+
}
44+
45+
@Override
46+
public boolean hasNext() {
47+
for ( ;position < emplyeelist.size(); position++) {
48+
if ("f".equalsIgnoreCase ((emplyeelist.get(position)).getSex())) {
49+
return true;
50+
}
51+
}
52+
return false;
53+
}
54+
55+
@Override
56+
public Employee next() {
57+
Employee employee = emplyeelist.get(position);
58+
position++;
59+
return employee;
60+
}
61+
62+
@Override
63+
public void remove() {
64+
emplyeelist.iterator().remove();
65+
}
66+
}
67+
68+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
Intent
2+
Explicitly separate the notion of "algorithm" from that of "data structure".
3+
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
4+
The key idea is to take the responsibility for access and traversal out of the aggregate object and put it into an Iterator object that defines a standard traversal protocol.
5+
6+
7+
Problem solved by pattern
8+
Need to "abstract" the traversal of wildly different data structures so that algorithms can be defined that are capable of interfacing with each transparently.
9+
10+
11+
12+
"An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you need to accomplish. But you probably don't want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you'll require. You might also need to have more than one traversal pending on the same list." And, providing a uniform interface for traversing many types of aggregate objects (i.e. polymorphic iteration) might be valuable.
13+
14+
The Iterator pattern lets you do all this. The key idea is to take the responsibility for access and traversal out of the aggregate object and put it into an Iterator object that defines a standard traversal protocol.
15+
16+
The Iterator abstraction is fundamental to an emerging technology called "generic programming". This strategy seeks to explicitly separate the notion of "algorithm" from that of "data structure". The motivation is to: promote component-based development, boost productivity, and reduce configuration management.
17+
18+
Key Notes on Iterator in Java:
19+
Iterator in Java basically is used to support generics thereby making it mandatory to make use of the Generic version of Iterator and not the Iterator with raw type.
20+
In case the objects need to be removed from the Collection, in that case do not make use of for-each loop. Instead one can make use of Iterator's remove() method to avoid any ConcurrentModificationException.
21+
As far as Iterating over collection with the help of Iterator concerned, it is subject to ConcurrentModificationException if Collection is modified after Iteration started. However this is meant to take place only in case of fail-fast Iterators.
22+
We have fail-fast and fail-safe type of Iterators in Java and you need to check for the difference between these types.
23+
List collection type is also known to supports ListIterator that comprises of add() method so as to incorporate elements in collection at the time of iteration. Difference between Iterator and ListIterator has also been discussed above.
24+
25+
26+
Read more: http://mrbool.com/how-to-create-iterator-in-java/26422#ixzz3DMNSrEMN
27+
28+
29+
External vs Internal Iterator
30+
31+
When the client controls the iteration sequence and index position then it is called as the external iterator. Otherwise if the iterator controls the traversal then it is called internal iterator. On external iterator, the design is that the invoking client code should explicitly invoke the method to move the pointer to next element. For internal iterator, when an element is accessed, after access the pointer will be automatically moved to next index by the iterator. In general internal iterators are easier to use than the external iterators.

0 commit comments

Comments
 (0)