Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator pattern #15

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Iterator Design Pattern class diagram.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Iterator pattern sequence diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
# DesignPatternsJava9
This repo consists Gang of Four Design patterns code on Java 9. Each branch in the repository has code of 1 design pattern. Switch repository to try out different design patterns.
# What is Iterator Design Pattern
Iterator Design Pattern enables to get a way to access the elements of a collection object in sequential manner without any need to know its underlying representation.

## Diagram
![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/iterator-pattern/diagrams/Iterator%20Design%20Pattern%20class%20diagram.jpeg "Diagram")

![Diagram](https://github.com/premaseem/DesignPatternsJava9/blob/iterator-pattern/diagrams/IteratorPatternSequenceDiagram.png "Diagram")

### When to use Iterator Design Pattern
* When there is a need to take the responsibility for access and traversal out of the aggregate / collection object and define a standard traversal protocol.
* Iterator pattern is widely used in Java Collection Framework.

### Learn Design Patterns with Java by Aseem Jain
This repository contains working project code used in video Course by Packt Publication with title "Learn Design Patterns with Java " authored by "Aseem Jain".

### Course link:
https://www.packtpub.com/application-development/learn-design-patterns-java-9-video

### ![ http://in.linkedin.com/in/premaseem](https://github.com/premaseem/DesignPatternsJava9/blob/master/linkedin.png "http://in.linkedin.com/in/premaseem") Profile: http://in.linkedin.com/in/premaseem

### Authors blog on design patterns:
https://premaseem.wordpress.com/category/computers/design-patterns/

### Software Design pattern community face book page:
https://www.facebook.com/DesignPatternGuru/

### Note:
* This code base will work on Java 9 and above versions.
* `diagrams` folders carry UML diagrams.
* `pattern` folder has code of primary example.
* `patternBonus` folder has code of secondary or bonus example.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/IteratorPatternSequenceDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/facebook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/facebooklist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 49 additions & 2 deletions pattern/src/com/premaseem/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,57 @@
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
@copyright: 2018 Packt Publication
*/
public class Client {
public static void main (String[] args) {
System.out.println("Singleton cook example ");
System.out.println("Iterator Pattern BEFORE CODE");

FaceBook faceBook = new FaceBook();
Iterator faceBookIterator = faceBook.getIterator();

// Loop over
// init data with first(), keep increment with next()
// and check termination condition with hasNext()
for(faceBookIterator.first();faceBookIterator.hasNext();faceBookIterator.next()){
System.out.println(faceBookIterator.currentValue());
}

// $$$ Lesson Learned $$$
// Loose coupling no need to know or import data structure
// Data cannot be compromised ;-)
// Provides limited control over data access



/* OLD CODE
// Data structure is exposed
List<String> friendsList = faceBook.getFriendsList();

// print friends with normal conventional loop
for (int i = 0; i < friendsList.size(); i++) {
System.out.println(friendsList.get(i));
}

// Un intentional addition of data
friendsList.add("Lady gaga");

// print friends with normal conventional loop
for (int i = 0; i < friendsList.size(); i++) {
System.out.println(friendsList.get(i));
}

// Clients can accidentally or maliciously trash data structure.
friendsList.clear();

// print friends with normal conventional loop
for (int i = 0; i < friendsList.size(); i++) {
System.out.println(friendsList.get(i));
}
*/
// Lesson Learned
// 1. Underlying Data structure is exposed
// 2. Data can be manipulated
// 3. Data can be get trashed accidentally
// 4. Data traversal is not easy or uniform
}
}
62 changes: 62 additions & 0 deletions pattern/src/com/premaseem/FaceBook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.premaseem;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;

/*
@author: Aseem Jain
@title: Design Patterns with Java 9
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
*/
public class FaceBook {

List<String> friendsList = new ArrayList<>();

public FaceBook () {
// initialize with seed data
friendsList.add("Aseem Jain");
friendsList.add("James Bond");
friendsList.add("Spider Man");
friendsList.add("Super Man");
}

// public List<String> getFriendsList () {
// return friendsList;
// }

public Iterator getIterator() {
return new Iterator(this);
}
}

class Iterator {
private FaceBook faceBook;
private java.util.Iterator iterator;
private String value;

public Iterator(FaceBook faceBook) {
this.faceBook = faceBook;
}

public void first() {
iterator = faceBook.friendsList.iterator();
next();
}

public void next() {
try {
value = (String)iterator.next();
} catch (NoSuchElementException ex) {
value = null;
}
}

public boolean hasNext() {
return value != null;
}

public String currentValue() {
return value;
}
}
13 changes: 0 additions & 13 deletions patternBonus/src/com/premaseem/Client.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.premaseem.iterator;

import java.util.Iterator;
import java.util.Map.Entry;

public class ClientForEmployeeIterator {

public static void main(String[] args) {
EmployeeManger employeeManager = new EmployeeManger();

System.out
.println("This is Iterator Pattern example where you can list all the male / female employee list using iterators. " +
"The data structure details are abstracted behind the iterator interface ");

employeeManager.addEmployee(new Employee("name1", "M"));
employeeManager.addEmployee(new Employee("name2", "f"));
employeeManager.addEmployee(new Employee("name3", "M"));
employeeManager.addEmployee(new Employee("name4", "Mf"));
employeeManager.addEmployee(new Employee("name5", "f"));
employeeManager.addEmployee(new Employee("name6", "f"));
employeeManager.addEmployee(new Employee("name7", "fM"));
employeeManager.addEmployee(new Employee("name8", "m"));
employeeManager.addEmployee(new Employee("name9", "m"));
employeeManager.addEmployee(new Employee("name10", "f"));

employeeManager.employeeMap.put(1,new Employee("name1", "M"));
employeeManager.employeeMap.put(2,new Employee("name2", "F"));
employeeManager.employeeMap.put(3,new Employee("name3", "M"));
employeeManager.employeeMap.put(1,new Employee("name4", "F"));

MaleEmployeeIterator maleEmployeeIterator = employeeManager.getMaleEmployeeIterator();
printEmployeeList(maleEmployeeIterator);

FeMaleEmployeeIterator feMaleEmployeeIterator = employeeManager.getFeMaleEmployeeIterator();
printEmployeeList(feMaleEmployeeIterator);

Iterator<Employee> allEmployeeIterator = employeeManager.getAllEmployeeIterator();
printEmployeeList(allEmployeeIterator);

printEmplyeeMap(employeeManager.printEmployeeMap());

}

private static void printEmplyeeMap(Iterator<Entry<Integer, Employee>> iterator) {
System.out.println("Printing employee map ");
while (iterator.hasNext()) {
Entry<Integer, Employee> next = iterator.next();
System.out.println(next);
}
}

private static void printEmployeeList(Iterator iterator) {
System.out.println("Printing the list for " + iterator.getClass().getSimpleName());
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}

}
25 changes: 25 additions & 0 deletions patternBonus/src/com/premaseem/iterator/Employee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.premaseem.iterator;

public class Employee {

String name;
String sex;

public Employee(String name, String sex){
this.name = name;
this.sex = sex;
}

public String getName() {
return name;
}

public String getSex() {
return sex;
}

@Override
public String toString() {
return sex + " " + name ;
}
}
43 changes: 43 additions & 0 deletions patternBonus/src/com/premaseem/iterator/EmployeeManger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.premaseem.iterator;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class EmployeeManger {

List<Employee> employeelist = new ArrayList<Employee>();
Map<Integer, Employee> employeeMap = new HashMap<Integer, Employee>();

void addEmployee(Employee emp) {
employeelist.add(emp);
}

boolean removeEmployee(Employee emp) {
return employeelist.remove(emp);
}

MaleEmployeeIterator getMaleEmployeeIterator() {
return new MaleEmployeeIterator(employeelist);
}

FeMaleEmployeeIterator getFeMaleEmployeeIterator() {
return new FeMaleEmployeeIterator(employeelist);
}

Iterator<Employee> getAllEmployeeIterator() {
return employeelist.iterator();
}

public Iterator<Entry<Integer, Employee>> printEmployeeMap() {
return employeeMap.entrySet().iterator();
}

void calculateEmpSalary() {

}

}
68 changes: 68 additions & 0 deletions patternBonus/src/com/premaseem/iterator/MaleEmployeeIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.premaseem.iterator;

import java.util.Iterator;
import java.util.List;

public class MaleEmployeeIterator implements Iterator<Employee> {

private List<Employee> employeelist;
private int position;
public MaleEmployeeIterator (List<Employee> employeelist){
this.employeelist = employeelist;
}

@Override
public boolean hasNext() {
for (; position < employeelist.size(); position++) {
if ("m".equalsIgnoreCase ((employeelist.get(position)).getSex())) {
return true;
}
}
return false;
}

@Override
public Employee next() {
Employee employee = employeelist.get(position);
position++;
return employee;
}

@Override
public void remove() {
employeelist.iterator().remove();
}
}

class FeMaleEmployeeIterator implements Iterator<Employee> {

private List<Employee> employeelist;
private int position;
public FeMaleEmployeeIterator (List<Employee> employeelist){
this.employeelist = employeelist;
}

@Override
public boolean hasNext() {
for (; position < employeelist.size(); position++) {
if ("f".equalsIgnoreCase ((employeelist.get(position)).getSex())) {
return true;
}
}
return false;
}

@Override
public Employee next() {
Employee employee = employeelist.get(position);
position++;
return employee;
}

@Override
public void remove() {
employeelist.iterator().remove();
}
}


Loading