Advertisement
MatthijsFontys

Iterator Composite pattern example

Jan 21st, 2021
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.65 KB | None | 0 0
  1. public class MyDiner{
  2.  
  3.     private string[] _menuitems;
  4.     private int _position;
  5.     private IEnumerable<string> _subMenu;
  6.  
  7.     public MyDiner(ICollection<string> subMenu) : IEnumerable{
  8.         _position = 0;
  9.         _menuItems = new string[] {"Coffee", "Latte machiato", "Cappuchino"};
  10.         _submenu = submenu.Iterator();
  11.     }
  12.  
  13.  
  14.     public bool HasNext(){
  15.         return _submenu.HasNext();
  16.     }
  17.  
  18.  
  19.     public string Next(){
  20.         if(_position < _menuitems.length){
  21.             _position ++;
  22.             return = __menuItems[_position - 1];
  23.         }
  24.         else if(_submenu.HasNext()){
  25.             return _submenu.Next();
  26.         }
  27.         else {
  28.             thow new IllegalOperationException("I'm out of elements!");
  29.         }
  30.     }
  31.    
  32.  
  33.  
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement