|
| 1 | +--- |
| 2 | +title: Iterator |
| 3 | +category: Behavioral |
| 4 | +language: it |
| 5 | +tag: |
| 6 | + - Gang of Four |
| 7 | +--- |
| 8 | + |
| 9 | +## Anche conosciuto come |
| 10 | + |
| 11 | +Cursor |
| 12 | + |
| 13 | +## Intento |
| 14 | +Fornire un modo per accedere agli elementi di un oggetto aggregato in modo sequenziale senza esporre la sua rappresentazione sottostante. |
| 15 | + |
| 16 | +## Spiegazione |
| 17 | + |
| 18 | +Esempio del mondo reale |
| 19 | + |
| 20 | +> Lo scrigno del tesoro contiene una serie di oggetti magici. Ci sono diversi tipi di oggetti, come anelli, |
| 21 | +> pozioni e armi. Gli oggetti possono essere esaminati per tipo utilizzando un iteratore fornito dalla cassa |
| 22 | +> del tesoro. |
| 23 | +
|
| 24 | +In parole semplici |
| 25 | + |
| 26 | +> I contenitori possono fornire un'interfaccia di iterazione agnostica alla rappresentazione per consentire l'accesso agli |
| 27 | +> elementi. |
| 28 | +
|
| 29 | +Wikipedia dice |
| 30 | + |
| 31 | +> Nella programmazione orientata agli oggetti, il pattern iterator è un design pattern in cui un iteratore viene |
| 32 | +> utilizzato per attraversare un contenitore e accedere agli elementi del contenitore stesso. _(Testo tradotto dalla voce Iterator Pattern da Wikipedia in lingua inglese)._ |
| 33 | +
|
| 34 | +**Esempio di codice** |
| 35 | + |
| 36 | +La classe principale nel nostro esempio è `TreasureChest`, che contiene gli oggetti. |
| 37 | + |
| 38 | +```java |
| 39 | +public class TreasureChest { |
| 40 | + |
| 41 | + private final List<Item> items; |
| 42 | + |
| 43 | + public TreasureChest() { |
| 44 | + items = List.of( |
| 45 | + new Item(ItemType.POTION, "Potion of courage"), |
| 46 | + new Item(ItemType.RING, "Ring of shadows"), |
| 47 | + new Item(ItemType.POTION, "Potion of wisdom"), |
| 48 | + new Item(ItemType.POTION, "Potion of blood"), |
| 49 | + new Item(ItemType.WEAPON, "Sword of silver +1"), |
| 50 | + new Item(ItemType.POTION, "Potion of rust"), |
| 51 | + new Item(ItemType.POTION, "Potion of healing"), |
| 52 | + new Item(ItemType.RING, "Ring of armor"), |
| 53 | + new Item(ItemType.WEAPON, "Steel halberd"), |
| 54 | + new Item(ItemType.WEAPON, "Dagger of poison")); |
| 55 | + } |
| 56 | + |
| 57 | + public Iterator<Item> iterator(ItemType itemType) { |
| 58 | + return new TreasureChestItemIterator(this, itemType); |
| 59 | + } |
| 60 | + |
| 61 | + public List<Item> getItems() { |
| 62 | + return new ArrayList<>(items); |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +Ecco la classe `Item`: |
| 68 | + |
| 69 | +```java |
| 70 | +public class Item { |
| 71 | + |
| 72 | + private ItemType type; |
| 73 | + private final String name; |
| 74 | + |
| 75 | + public Item(ItemType type, String name) { |
| 76 | + this.setType(type); |
| 77 | + this.name = name; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String toString() { |
| 82 | + return name; |
| 83 | + } |
| 84 | + |
| 85 | + public ItemType getType() { |
| 86 | + return type; |
| 87 | + } |
| 88 | + |
| 89 | + public final void setType(ItemType type) { |
| 90 | + this.type = type; |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +public enum ItemType { |
| 95 | + |
| 96 | + ANY, WEAPON, RING, POTION |
| 97 | + |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +L'interfaccia `Iterator` è estremamente semplice. |
| 102 | + |
| 103 | +```java |
| 104 | +public interface Iterator<T> { |
| 105 | + |
| 106 | + boolean hasNext(); |
| 107 | + |
| 108 | + T next(); |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Nell'esempio seguente, si iterano gli oggetti di tipo anello che si trovano nel forziere. |
| 113 | + |
| 114 | +```java |
| 115 | +var itemIterator = TREASURE_CHEST.iterator(ItemType.RING); |
| 116 | +while (itemIterator.hasNext()) { |
| 117 | + LOGGER.info(itemIterator.next().toString()); |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Output del programma: |
| 122 | + |
| 123 | +```java |
| 124 | +Ring of shadows |
| 125 | +Ring of armor |
| 126 | +``` |
| 127 | + |
| 128 | +## Diagramma delle classi |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +## Applicabilità |
| 133 | + |
| 134 | +Usa il pattern Iterator |
| 135 | + |
| 136 | +* Per accedere ai contenuti di un oggetto aggregato senza esporre la sua rappresentazione interna. |
| 137 | +* Per supportare più attraversamenti di oggetti aggregati. |
| 138 | +* Per fornire un'interfaccia uniforme per l'attraversamento di diverse strutture aggregate. |
| 139 | + |
| 140 | +## Tutorial |
| 141 | + |
| 142 | +* [How to Use Iterator?](http://www.tutorialspoint.com/java/java_using_iterator.htm) |
| 143 | + |
| 144 | +## Usi noti |
| 145 | + |
| 146 | +* [java.util.Iterator](http://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html) |
| 147 | +* [java.util.Enumeration](http://docs.oracle.com/javase/8/docs/api/java/util/Enumeration.html) |
| 148 | + |
| 149 | +## Collegamenti esterni |
| 150 | + |
| 151 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) |
| 152 | +* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) |
0 commit comments