From 3945b744dcd3e87f06ad698892556dd6a9fd7d52 Mon Sep 17 00:00:00 2001 From: peterm85 Date: Fri, 22 Nov 2019 11:48:30 +0100 Subject: [PATCH] Add Iterator pattern new examples --- README.md | 3 +- .../examples/notifications/Client.java | 9 ++++++ .../examples/notifications/Collection.java | 5 +++ .../examples/notifications/Iterator.java | 7 ++++ .../examples/notifications/Notification.java | 12 +++++++ .../notifications/NotificationBar.java | 18 +++++++++++ .../notifications/NotificationCollection.java | 32 +++++++++++++++++++ .../notifications/NotificationIterator.java | 26 +++++++++++++++ 8 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 src/iterator/examples/notifications/Client.java create mode 100644 src/iterator/examples/notifications/Collection.java create mode 100644 src/iterator/examples/notifications/Iterator.java create mode 100644 src/iterator/examples/notifications/Notification.java create mode 100644 src/iterator/examples/notifications/NotificationBar.java create mode 100644 src/iterator/examples/notifications/NotificationCollection.java create mode 100644 src/iterator/examples/notifications/NotificationIterator.java diff --git a/README.md b/README.md index 66f8213..2d4ca66 100644 --- a/README.md +++ b/README.md @@ -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/) diff --git a/src/iterator/examples/notifications/Client.java b/src/iterator/examples/notifications/Client.java new file mode 100644 index 0000000..e6337d6 --- /dev/null +++ b/src/iterator/examples/notifications/Client.java @@ -0,0 +1,9 @@ +package iterator.examples.notifications; + +public class Client { + + public static void main(String args[]) { + NotificationBar nb = new NotificationBar(); + nb.printNotifications(); + } +} diff --git a/src/iterator/examples/notifications/Collection.java b/src/iterator/examples/notifications/Collection.java new file mode 100644 index 0000000..1300133 --- /dev/null +++ b/src/iterator/examples/notifications/Collection.java @@ -0,0 +1,5 @@ +package iterator.examples.notifications; + +public interface Collection { + public Iterator createIterator(); +} diff --git a/src/iterator/examples/notifications/Iterator.java b/src/iterator/examples/notifications/Iterator.java new file mode 100644 index 0000000..6f8f18a --- /dev/null +++ b/src/iterator/examples/notifications/Iterator.java @@ -0,0 +1,7 @@ +package iterator.examples.notifications; + +public interface Iterator { + boolean hasNext(); + + Object next(); +} diff --git a/src/iterator/examples/notifications/Notification.java b/src/iterator/examples/notifications/Notification.java new file mode 100644 index 0000000..76b1ebd --- /dev/null +++ b/src/iterator/examples/notifications/Notification.java @@ -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; + } +} diff --git a/src/iterator/examples/notifications/NotificationBar.java b/src/iterator/examples/notifications/NotificationBar.java new file mode 100644 index 0000000..45fa419 --- /dev/null +++ b/src/iterator/examples/notifications/NotificationBar.java @@ -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()); + } + } +} diff --git a/src/iterator/examples/notifications/NotificationCollection.java b/src/iterator/examples/notifications/NotificationCollection.java new file mode 100644 index 0000000..1a9965e --- /dev/null +++ b/src/iterator/examples/notifications/NotificationCollection.java @@ -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); + } + +} diff --git a/src/iterator/examples/notifications/NotificationIterator.java b/src/iterator/examples/notifications/NotificationIterator.java new file mode 100644 index 0000000..cc9051f --- /dev/null +++ b/src/iterator/examples/notifications/NotificationIterator.java @@ -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; + } + } +}