atanasovetr

IteratorPattern_GFG

May 26th, 2021
1,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. // A Java program to demonstrate implementation
  2. // of iterator pattern with the example of
  3. // notifications
  4.  
  5. // A simple Notification class
  6. class Notification
  7. {
  8.     // To store notification message
  9.     String notification;
  10.  
  11.     public Notification(String notification)
  12.     {
  13.         this.notification = notification;
  14.     }
  15.     public String getNotification()
  16.     {
  17.         return notification;
  18.     }
  19. }
  20.  
  21. // Collection interface
  22. interface Collection
  23. {
  24.     public Iterator createIterator();
  25. }
  26.  
  27. // Collection of notifications
  28. class NotificationCollection implements Collection
  29. {
  30.     static final int MAX_ITEMS = 6;
  31.     int numberOfItems = 0;
  32.     Notification[] notificationList;
  33.  
  34.     public NotificationCollection()
  35.     {
  36.         notificationList = new Notification[MAX_ITEMS];
  37.  
  38.         // Let us add some dummy notifications
  39.         addItem("Notification 1");
  40.         addItem("Notification 2");
  41.         addItem("Notification 3");
  42.     }
  43.  
  44.     public void addItem(String str)
  45.     {
  46.         Notification notification = new Notification(str);
  47.         if (numberOfItems >= MAX_ITEMS)
  48.             System.err.println("Full");
  49.         else
  50.         {
  51.             notificationList[numberOfItems] = notification;
  52.             numberOfItems = numberOfItems + 1;
  53.         }
  54.     }
  55.  
  56.     public Iterator createIterator()
  57.     {
  58.         return new NotificationIterator(notificationList);
  59.     }
  60. }
  61.  
  62. // We could also use Java.Util.Iterator
  63. interface Iterator
  64. {
  65.     // indicates whether there are more elements to
  66.     // iterate over
  67.     boolean hasNext();
  68.  
  69.     // returns the next element
  70.     Object next();
  71. }
  72.  
  73. // Notification iterator
  74. class NotificationIterator implements Iterator
  75. {
  76.     Notification[] notificationList;
  77.  
  78.     // maintains curr pos of iterator over the array
  79.     int pos = 0;
  80.  
  81.     // Constructor takes the array of notifiactionList are
  82.     // going to iterate over.
  83.     public  NotificationIterator (Notification[] notificationList)
  84.     {
  85.         this.notificationList = notificationList;
  86.     }
  87.  
  88.     public Object next()
  89.     {
  90.         // return next element in the array and increment pos
  91.         Notification notification =  notificationList[pos];
  92.         pos += 1;
  93.         return notification;
  94.     }
  95.  
  96.     public boolean hasNext()
  97.     {
  98.         if (pos >= notificationList.length ||
  99.             notificationList[pos] == null)
  100.             return false;
  101.         else
  102.             return true;
  103.     }
  104. }
  105.  
  106. // Contains collection of notifications as an object of
  107. // NotificationCollection
  108. class NotificationBar
  109. {
  110.     NotificationCollection notifications;
  111.  
  112.     public NotificationBar(NotificationCollection notifications)
  113.     {
  114.         this.notifications = notifications;
  115.     }
  116.  
  117.     public void printNotifications()
  118.     {
  119.         Iterator iterator = notifications.createIterator();
  120.         System.out.println("-------NOTIFICATION BAR------------");
  121.         while (iterator.hasNext())
  122.         {
  123.             Notification n = (Notification)iterator.next();
  124.             System.out.println(n.getNotification());
  125.         }
  126.     }
  127. }
  128.  
  129. // Driver class
  130. class Main
  131. {
  132.     public static void main(String args[])
  133.     {
  134.         NotificationCollection nc = new NotificationCollection();
  135.         NotificationBar nb = new NotificationBar(nc);
  136.         nb.printNotifications();
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment