Skip to content

Commit

Permalink
Add Iterator pattern new examples
Browse files Browse the repository at this point in the history
  • Loading branch information
peterm85 committed Nov 22, 2019
1 parent 5ee7423 commit 3945b74
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,6 @@ Primero(), Siguiente(), HayMas() y ElementoActual().

**Ejemplos:**
* [Vectors](https://github.com/peterm85/design-patterns/tree/master/src/iterator/examples/vectors)
* [Notifications](https://github.com/peterm85/design-patterns/tree/master/src/iterator/examples/notifications)

**Referencia:** [https://www.geeksforgeeks.org/template-method-design-pattern/](https://www.geeksforgeeks.org/template-method-design-pattern/)
**Referencia:** [https://www.geeksforgeeks.org/iterator-pattern/](https://www.geeksforgeeks.org/iterator-pattern/)
9 changes: 9 additions & 0 deletions src/iterator/examples/notifications/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package iterator.examples.notifications;

public class Client {

public static void main(String args[]) {
NotificationBar nb = new NotificationBar();
nb.printNotifications();
}
}
5 changes: 5 additions & 0 deletions src/iterator/examples/notifications/Collection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package iterator.examples.notifications;

public interface Collection {
public Iterator createIterator();
}
7 changes: 7 additions & 0 deletions src/iterator/examples/notifications/Iterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package iterator.examples.notifications;

public interface Iterator {
boolean hasNext();

Object next();
}
12 changes: 12 additions & 0 deletions src/iterator/examples/notifications/Notification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package iterator.examples.notifications;

public class Notification {
String notification;

public Notification(String notification) {
this.notification = notification;
}
public String getNotification() {
return notification;
}
}
18 changes: 18 additions & 0 deletions src/iterator/examples/notifications/NotificationBar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package iterator.examples.notifications;

public class NotificationBar {
NotificationCollection notifications;

public NotificationBar() {
this.notifications = new NotificationCollection();
}

public void printNotifications() {
Iterator iterator = notifications.createIterator();
System.out.println("-------NOTIFICATION BAR------------");
while (iterator.hasNext()) {
Notification n = (Notification)iterator.next();
System.out.println(n.getNotification());
}
}
}
32 changes: 32 additions & 0 deletions src/iterator/examples/notifications/NotificationCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package iterator.examples.notifications;

public class NotificationCollection implements Collection {

static final int MAX_ITEMS = 6;
int numberOfItems = 0;
Notification[] notificationList;

public NotificationCollection() {
notificationList = new Notification[MAX_ITEMS];

// Let us add some dummy notifications
addItem("Notification 1");
addItem("Notification 2");
addItem("Notification 3");
}

public void addItem(String str) {
Notification notification = new Notification(str);
if (numberOfItems >= MAX_ITEMS) {
System.err.println("Full");
} else {
notificationList[numberOfItems] = notification;
numberOfItems = numberOfItems + 1;
}
}

public Iterator createIterator() {
return new NotificationIterator(notificationList);
}

}
26 changes: 26 additions & 0 deletions src/iterator/examples/notifications/NotificationIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package iterator.examples.notifications;

public class NotificationIterator implements Iterator {
Notification[] notificationList;

int pos = 0;

public NotificationIterator (Notification[] notificationList) {
this.notificationList = notificationList;
}

public Object next() {
Notification notification = notificationList[pos];
pos += 1;
return notification;
}

public boolean hasNext() {
if (pos >= notificationList.length ||
notificationList[pos] == null) {
return false;
} else {
return true;
}
}
}

0 comments on commit 3945b74

Please sign in to comment.