forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package iterator.examples.notifications; | ||
|
||
public interface Collection { | ||
public Iterator createIterator(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
src/iterator/examples/notifications/NotificationCollection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
src/iterator/examples/notifications/NotificationIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |