Knowledge (XXG)

Iterator pattern

Source đź“ť

235: 273:
can immediately be applied to plain old memory buffers, and that there is no new syntax to learn. However, it requires an "end" iterator to test for equality, rather than allowing an iterator to know that it has reached the end. In C++ language, we say that an iterator models the iterator
86:
Defining access and traversal operations in the aggregate interface is inflexible because it commits the aggregate to particular access and traversal operations and makes it impossible to add new operations later without having to change the aggregate interface.
268:
in that language. In C++, a class can overload all of the pointer operations, so an iterator can be implemented that acts more or less like a pointer, complete with dereference, increment, and decrement. This has the advantage that C++ algorithms such as
68:
that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
116:
The essence of the Iterator Pattern is to "Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.".
130: 1400: 1327: 50:
can be implemented generally using a specified type of iterator rather than implementing it as a container-specific algorithm. This allows
1925: 2038: 2033: 1950: 1243: 1196: 28: 1723: 1358: 1898: 1522: 36: 1706: 1616: 265: 1746: 1716: 1711: 79:
The elements of an aggregate object should be accessed and traversed without exposing its representation (data structures).
1393: 275: 1991: 1829: 20: 1606: 1191: 1368: 1814: 1809: 1636: 99:
Clients use an iterator to access and traverse an aggregate without knowing its representation (data structures).
1854: 1819: 1786: 1436: 1386: 1651: 1756: 1728: 1666: 1631: 1567: 1409: 43:
from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.
129: 1733: 1661: 1611: 1446: 1282: 2012: 1915: 1761: 1741: 1686: 1824: 1781: 1776: 1766: 1676: 1864: 1849: 1844: 1701: 1586: 1532: 96:
Define a separate (iterator) object that encapsulates accessing and traversing an aggregate object.
82:
New traversal operations should be defined for an aggregate object without changing its interface.
1986: 1965: 1874: 1771: 1621: 1514: 1466: 1428: 1656: 1499: 1489: 1484: 1456: 1451: 1323: 1239: 1235: 1228: 1186: 1955: 1696: 1641: 1562: 1552: 1542: 1346: 1206: 234: 191: 105:
New access and traversal operations can be defined independently by defining new iterators.
1945: 1891: 1869: 1646: 1601: 1572: 1547: 1527: 1474: 1441: 1417: 64: 1352: 103:
Different iterators can be used to access and traverse an aggregate in different ways.
1930: 1791: 1494: 1479: 2027: 1751: 1596: 1557: 1504: 141: 2007: 1970: 1859: 1834: 1626: 1363: 1960: 1935: 1920: 1839: 1671: 281:
This C++11 implementation is based on chapter "Generalizing vector yet again".
1294: 1259: 1940: 40: 1378: 1373: 1201: 248: 54:
to be used on any container that supports the required type of iterator.
32: 253:
Some languages standardize syntax. C++ and Python are notable examples.
133:
A sample UML class and sequence diagram for the Iterator design pattern.
1260:"The Iterator design pattern - Problem, Solution, and Applicability" 39:
and access the container's elements. The iterator pattern decouples
1359:
Iterator pattern in UML and in LePUS3 (a formal modelling language)
261: 233: 128: 1226:
Erich Gamma; Richard Helm; Ralph Johnson; John Vlissides (1994).
1382: 1230:
Design Patterns: Elements of Reusable Object-Oriented Software
188: 138: 1295:"The Iterator design pattern - Structure and Collaboration" 62:
The Iterator design pattern is one of the 23 well-known
91:
What solution does the Iterator design pattern describe?
72:
What problems can the Iterator design pattern solve?
2000: 1979: 1908: 1883: 1800: 1685: 1585: 1513: 1465: 1427: 1416: 108:See also the UML class and sequence diagram below. 1227: 1369:Design Patterns implementation examples tutorial 1322:(2 ed.). Addison Wesley. pp. 729 ff. 1320:Programming: Principles and Practice using C++ 1394: 8: 264:implements iterators with the semantics of 1424: 1401: 1387: 1379: 46:For example, the hypothetical algorithm 1218: 194:shows the run-time interactions: The 7: 14: 1197:Design pattern (computer science) 222:to traverse the elements of the 1899:Enterprise Integration Patterns 125:UML class and sequence diagram 65:"Gang of Four" design patterns 1: 210:object and returns it to the 712:"Vector::operator" 164:interface for traversing an 1992:Portland Pattern Repository 1234:. Addison Wesley. pp.  180:interface by accessing the 21:object-oriented programming 2055: 1318:Bjarne Stroustrup (2014). 1192:Container (data structure) 246: 152:interface for creating an 206:object, which creates an 2039:Software design patterns 2034:Iteration in programming 1617:Event-based asynchronous 1410:Software design patterns 1103: 301:<initializer_list> 283: 148:class refers (1) to the 1523:Chain of responsibility 16:Software design pattern 1662:Scheduled-task pattern 1612:Double-checked locking 1101:The program output is 239: 134: 35:is used to traverse a 2013:Architectural pattern 1916:Christopher Alexander 1364:SourceMaking tutorial 237: 176:class implements the 132: 1825:Dependency injection 1782:Inversion of control 1777:Data transfer object 1677:Thread-local storage 238:The iterator pattern 1830:Intercepting filter 1987:The Hillside Group 1772:Data access object 1622:Guarded suspension 1607:Binding properties 240: 135: 2021: 2020: 1815:Business delegate 1747:Publish–subscribe 1581: 1580: 1329:978-0-321-99278-9 1187:Composite pattern 295:<stdexcept> 230:UML class diagram 160:) and (2) to the 2046: 1820:Composite entity 1697:Front controller 1437:Abstract factory 1425: 1403: 1396: 1389: 1380: 1374:Iterator Pattern 1353:Iterator Pattern 1347:Object iteration 1334: 1333: 1315: 1309: 1308: 1306: 1305: 1291: 1285: 1280: 1274: 1273: 1271: 1270: 1256: 1250: 1249: 1233: 1223: 1207:Observer pattern 1176: 1173: 1170: 1167: 1164: 1161: 1158: 1155: 1152: 1149: 1146: 1143: 1140: 1137: 1134: 1131: 1128: 1125: 1122: 1119: 1116: 1113: 1110: 1107: 1097: 1094: 1091: 1088: 1085: 1082: 1079: 1076: 1073: 1070: 1067: 1064: 1061: 1058: 1055: 1052: 1049: 1046: 1043: 1040: 1037: 1034: 1031: 1028: 1025: 1022: 1019: 1016: 1013: 1010: 1007: 1004: 1001: 998: 995: 992: 989: 986: 983: 980: 977: 974: 971: 968: 965: 962: 959: 956: 953: 950: 947: 944: 941: 938: 935: 932: 929: 926: 923: 920: 917: 914: 911: 908: 905: 902: 899: 896: 893: 890: 887: 884: 881: 878: 875: 872: 869: 866: 863: 860: 857: 854: 851: 848: 845: 842: 839: 836: 833: 830: 827: 824: 821: 818: 815: 812: 809: 806: 803: 800: 797: 794: 791: 788: 785: 782: 779: 776: 773: 770: 767: 764: 761: 758: 757:// rule of three 755: 752: 749: 746: 743: 740: 737: 734: 731: 728: 725: 722: 719: 716: 713: 710: 707: 704: 701: 698: 695: 692: 689: 686: 683: 680: 677: 674: 671: 668: 665: 662: 659: 656: 653: 650: 647: 644: 641: 638: 635: 632: 629: 626: 623: 620: 617: 614: 611: 608: 605: 602: 599: 596: 593: 590: 587: 584: 581: 578: 575: 572: 569: 566: 563: 560: 557: 554: 551: 548: 545: 542: 539: 536: 533: 530: 527: 524: 521: 518: 515: 512: 509: 506: 503: 500: 497: 494: 491: 488: 485: 482: 479: 476: 473: 470: 467: 464: 461: 458: 455: 452: 449: 446: 443: 440: 437: 434: 431: 428: 425: 422: 419: 416: 413: 410: 407: 404: 403:initializer_list 401: 398: 395: 392: 389: 386: 383: 380: 377: 374: 371: 368: 365: 362: 359: 356: 353: 350: 347: 344: 341: 338: 335: 332: 329: 326: 323: 320: 317: 314: 311: 308: 305: 302: 299: 296: 293: 290: 289:<iostream> 287: 272: 225: 221: 217: 213: 209: 205: 201: 200:createIterator() 197: 192:sequence diagram 183: 179: 175: 171: 170:next(),hasNext() 167: 163: 159: 158:createIterator() 155: 151: 147: 52:SearchForElement 48:SearchForElement 25:iterator pattern 2054: 2053: 2049: 2048: 2047: 2045: 2044: 2043: 2024: 2023: 2022: 2017: 1996: 1975: 1966:Douglas Schmidt 1946:Ward Cunningham 1904: 1892:Design Patterns 1879: 1870:Method chaining 1802: 1796: 1757:Service locator 1688: 1681: 1652:Read–write lock 1588: 1577: 1568:Template method 1509: 1461: 1419: 1412: 1407: 1343: 1338: 1337: 1330: 1317: 1316: 1312: 1303: 1301: 1293: 1292: 1288: 1281: 1277: 1268: 1266: 1258: 1257: 1253: 1246: 1225: 1224: 1220: 1215: 1183: 1178: 1177: 1174: 1171: 1168: 1165: 1162: 1159: 1156: 1153: 1150: 1147: 1144: 1141: 1138: 1135: 1132: 1129: 1126: 1123: 1120: 1117: 1114: 1111: 1108: 1105: 1099: 1098: 1095: 1092: 1089: 1086: 1083: 1080: 1077: 1074: 1071: 1068: 1065: 1062: 1059: 1056: 1053: 1050: 1047: 1044: 1041: 1038: 1035: 1032: 1029: 1026: 1023: 1020: 1017: 1014: 1011: 1008: 1005: 1002: 999: 996: 993: 990: 987: 984: 981: 978: 975: 972: 969: 966: 963: 960: 957: 954: 951: 948: 945: 942: 939: 936: 933: 930: 927: 924: 921: 918: 915: 912: 909: 906: 903: 900: 897: 894: 891: 888: 885: 882: 879: 876: 873: 870: 867: 864: 861: 858: 855: 852: 849: 846: 843: 840: 837: 834: 831: 828: 825: 822: 819: 816: 813: 810: 807: 804: 801: 798: 795: 792: 789: 786: 783: 780: 777: 774: 771: 768: 765: 762: 759: 756: 753: 750: 747: 744: 741: 738: 735: 732: 729: 726: 723: 720: 717: 714: 711: 708: 705: 702: 699: 696: 693: 690: 687: 684: 681: 678: 675: 672: 669: 666: 663: 660: 657: 654: 651: 648: 645: 642: 639: 636: 633: 630: 627: 624: 621: 618: 615: 612: 609: 606: 603: 600: 597: 594: 591: 588: 585: 582: 579: 576: 573: 570: 567: 564: 561: 558: 555: 552: 549: 546: 543: 540: 537: 534: 531: 528: 525: 522: 519: 516: 513: 510: 507: 504: 501: 498: 495: 492: 489: 486: 483: 480: 477: 474: 471: 468: 465: 462: 459: 456: 453: 450: 447: 444: 441: 438: 435: 432: 429: 426: 423: 420: 417: 414: 411: 408: 405: 402: 399: 396: 393: 390: 387: 384: 381: 378: 375: 372: 369: 366: 363: 360: 357: 354: 351: 348: 345: 342: 339: 336: 333: 330: 327: 324: 321: 318: 315: 312: 309: 306: 303: 300: 297: 294: 291: 288: 285: 270: 259: 251: 245: 232: 223: 219: 215: 211: 207: 203: 199: 195: 181: 177: 173: 169: 165: 161: 157: 153: 149: 145: 127: 122: 114: 104: 93: 74: 60: 17: 12: 11: 5: 2052: 2050: 2042: 2041: 2036: 2026: 2025: 2019: 2018: 2016: 2015: 2010: 2004: 2002: 1998: 1997: 1995: 1994: 1989: 1983: 1981: 1977: 1976: 1974: 1973: 1968: 1963: 1958: 1953: 1948: 1943: 1938: 1933: 1931:John Vlissides 1928: 1923: 1918: 1912: 1910: 1906: 1905: 1903: 1902: 1895: 1887: 1885: 1881: 1880: 1878: 1877: 1872: 1867: 1862: 1857: 1852: 1847: 1842: 1837: 1832: 1827: 1822: 1817: 1812: 1806: 1804: 1798: 1797: 1795: 1794: 1789: 1784: 1779: 1774: 1769: 1764: 1759: 1754: 1749: 1744: 1739: 1731: 1726: 1721: 1720: 1719: 1714: 1704: 1699: 1693: 1691: 1683: 1682: 1680: 1679: 1674: 1669: 1664: 1659: 1654: 1649: 1644: 1639: 1634: 1629: 1624: 1619: 1614: 1609: 1604: 1599: 1593: 1591: 1583: 1582: 1579: 1578: 1576: 1575: 1570: 1565: 1560: 1555: 1550: 1545: 1540: 1535: 1530: 1525: 1519: 1517: 1511: 1510: 1508: 1507: 1502: 1497: 1492: 1487: 1482: 1477: 1471: 1469: 1463: 1462: 1460: 1459: 1454: 1449: 1447:Factory method 1444: 1439: 1433: 1431: 1422: 1414: 1413: 1408: 1406: 1405: 1398: 1391: 1383: 1377: 1376: 1371: 1366: 1361: 1356: 1350: 1342: 1341:External links 1339: 1336: 1335: 1328: 1310: 1286: 1275: 1251: 1244: 1217: 1216: 1214: 1211: 1210: 1209: 1204: 1199: 1194: 1189: 1182: 1179: 1104: 284: 258: 255: 247:Main article: 244: 241: 231: 228: 126: 123: 121: 118: 113: 110: 101: 100: 97: 92: 89: 84: 83: 80: 73: 70: 59: 56: 29:design pattern 15: 13: 10: 9: 6: 4: 3: 2: 2051: 2040: 2037: 2035: 2032: 2031: 2029: 2014: 2011: 2009: 2006: 2005: 2003: 1999: 1993: 1990: 1988: 1985: 1984: 1982: 1978: 1972: 1969: 1967: 1964: 1962: 1959: 1957: 1956:Robert Martin 1954: 1952: 1951:Martin Fowler 1949: 1947: 1944: 1942: 1939: 1937: 1934: 1932: 1929: 1927: 1926:Ralph Johnson 1924: 1922: 1919: 1917: 1914: 1913: 1911: 1907: 1901: 1900: 1896: 1894: 1893: 1889: 1888: 1886: 1882: 1876: 1873: 1871: 1868: 1866: 1863: 1861: 1858: 1856: 1853: 1851: 1848: 1846: 1843: 1841: 1838: 1836: 1833: 1831: 1828: 1826: 1823: 1821: 1818: 1816: 1813: 1811: 1808: 1807: 1805: 1799: 1793: 1790: 1788: 1785: 1783: 1780: 1778: 1775: 1773: 1770: 1768: 1765: 1763: 1762:Active record 1760: 1758: 1755: 1753: 1752:Naked objects 1750: 1748: 1745: 1743: 1742:Specification 1740: 1738: 1736: 1732: 1730: 1727: 1725: 1722: 1718: 1715: 1713: 1710: 1709: 1708: 1705: 1703: 1700: 1698: 1695: 1694: 1692: 1690: 1687:Architectural 1684: 1678: 1675: 1673: 1670: 1668: 1665: 1663: 1660: 1658: 1655: 1653: 1650: 1648: 1645: 1643: 1640: 1638: 1635: 1633: 1630: 1628: 1625: 1623: 1620: 1618: 1615: 1613: 1610: 1608: 1605: 1603: 1600: 1598: 1597:Active object 1595: 1594: 1592: 1590: 1584: 1574: 1571: 1569: 1566: 1564: 1561: 1559: 1556: 1554: 1551: 1549: 1546: 1544: 1541: 1539: 1536: 1534: 1531: 1529: 1526: 1524: 1521: 1520: 1518: 1516: 1512: 1506: 1503: 1501: 1498: 1496: 1493: 1491: 1488: 1486: 1483: 1481: 1478: 1476: 1473: 1472: 1470: 1468: 1464: 1458: 1455: 1453: 1450: 1448: 1445: 1443: 1440: 1438: 1435: 1434: 1432: 1430: 1426: 1423: 1421: 1415: 1411: 1404: 1399: 1397: 1392: 1390: 1385: 1384: 1381: 1375: 1372: 1370: 1367: 1365: 1362: 1360: 1357: 1354: 1351: 1348: 1345: 1344: 1340: 1331: 1325: 1321: 1314: 1311: 1300: 1299:w3sDesign.com 1296: 1290: 1287: 1284: 1279: 1276: 1265: 1264:w3sDesign.com 1261: 1255: 1252: 1247: 1245:0-201-63361-2 1241: 1237: 1232: 1231: 1222: 1219: 1212: 1208: 1205: 1203: 1200: 1198: 1195: 1193: 1190: 1188: 1185: 1184: 1180: 1102: 282: 279: 277: 267: 263: 256: 254: 250: 242: 236: 229: 227: 198:object calls 193: 190: 185: 143: 142:class diagram 140: 137:In the above 131: 124: 119: 117: 111: 109: 106: 98: 95: 94: 90: 88: 81: 78: 77: 76: 71: 69: 67: 66: 57: 55: 53: 49: 44: 42: 38: 34: 30: 26: 22: 2008:Anti-pattern 1971:Linda Rising 1897: 1890: 1835:Lazy loading 1767:Identity map 1734: 1537: 1418:Gang of Four 1319: 1313: 1302:. Retrieved 1298: 1289: 1283:Gang Of Four 1278: 1267:. Retrieved 1263: 1254: 1229: 1221: 1154:out_of_range 1100: 1087:'\n' 1009:'\n' 922:'\n' 706:out_of_range 280: 260: 252: 186: 136: 115: 107: 102: 85: 75: 63: 61: 51: 47: 45: 31:in which an 24: 18: 1980:Communities 1961:Jim Coplien 1936:Grady Booch 1921:Erich Gamma 1865:Type tunnel 1850:Object pool 1845:Null object 1840:Mock object 1702:Interceptor 1672:Thread pool 1587:Concurrency 1533:Interpreter 2028:Categories 1875:Delegation 1810:Blackboard 1515:Behavioral 1467:Structural 1429:Creational 1304:2017-08-12 1269:2017-08-12 1213:References 224:Aggregate1 218:uses then 204:Aggregate1 182:Aggregate1 112:Definition 41:algorithms 1941:Kent Beck 1667:Semaphore 1657:Scheduler 1500:Flyweight 1490:Decorator 1485:Composite 1457:Singleton 1452:Prototype 1124:terminate 271:std::sort 220:Iterator1 208:Iterator1 174:Iterator1 166:Aggregate 150:Aggregate 120:Structure 37:container 2001:See also 1803:patterns 1689:patterns 1642:Proactor 1589:patterns 1563:Strategy 1553:Observer 1543:Mediator 1538:Iterator 1420:patterns 1202:Iterator 1181:See also 1175:operator 1139:instance 1133:throwing 1084:<< 1078:<< 1006:<< 997:<< 919:<< 913:<< 766:operator 649:operator 361:iterator 337:iterator 322:iterator 298:#include 292:#include 286:#include 266:pointers 249:Iterator 226:object. 178:Iterator 168:object ( 162:Iterator 156:object ( 154:Iterator 58:Overview 33:iterator 1855:Servant 1787:Model 2 1647:Reactor 1637:Monitor 1602:Balking 1573:Visitor 1548:Memento 1528:Command 1475:Adapter 1442:Builder 796:private 430:nullptr 276:concept 243:Example 184:class. 172:). The 1909:People 1792:Broker 1495:Facade 1480:Bridge 1349:in PHP 1326:  1242:  1169:Vector 1127:called 838:Vector 802:double 790:delete 778:Vector 760:Vector 751:delete 739:Vector 730:Vector 718:return 643:double 631:return 604:delete 595:Vector 484:double 478:double 409:double 391:Vector 373:return 349:return 328:double 313:public 307:Vector 216:Client 214:. The 212:Client 202:on an 196:Client 146:Client 144:, the 23:, the 1884:Books 1801:Other 1737:-tier 1558:State 1505:Proxy 1355:in C# 1236:257ff 1157:' 1145:' 1130:after 1042:<= 952:begin 886:& 880:const 781:& 775:const 763:& 742:& 736:const 697:throw 688:>= 646:& 625:const 523:begin 340:begin 319:using 304:class 27:is a 1860:Twin 1717:MVVM 1632:Lock 1627:Join 1324:ISBN 1240:ISBN 1160:what 1121:4.84 1118:1.21 1115:4.84 1112:1.21 1109:4.84 1106:1.21 1075:cout 1051:size 1024:auto 994:cout 937:auto 910:cout 883:auto 829:main 808:elem 721:elem 676:< 619:size 607:elem 508:auto 496:elem 469:elem 463:size 424:elem 412:> 406:< 376:elem 352:elem 187:The 1729:ECS 1724:ADR 1712:MVP 1707:MVC 1148:std 1069:std 1054:(); 1018:for 988:std 973:(); 970:end 955:(); 931:for 904:std 874:for 868:2.2 862:2.2 856:1.1 850:1.1 826:int 814:int 700:std 655:int 616:int 544:(); 541:end 535:lst 526:(); 517:lst 502:for 475:new 466:(); 457:lst 415:lst 397:std 364:end 262:C++ 257:C++ 189:UML 139:UML 19:In 2030:: 1297:. 1262:. 1238:. 1172::: 1163:() 1151::: 1142:of 1136:an 1072::: 1057:++ 991::: 976:++ 961:!= 907::: 871:}; 832:() 823:}; 817:sz 715:); 703::: 691:sz 682:|| 667:if 634:sz 622:() 598:() 556:++ 547:++ 532:!= 451:sz 436:sz 433:), 400::: 382:sz 367:() 343:() 278:. 1735:n 1402:e 1395:t 1388:v 1332:. 1307:. 1272:. 1248:. 1166:: 1096:} 1093:} 1090:; 1081:v 1066:{ 1063:) 1060:i 1048:. 1045:v 1039:i 1036:; 1033:0 1030:= 1027:i 1021:( 1015:} 1012:; 1003:i 1000:* 985:{ 982:) 979:i 967:. 964:v 958:i 949:. 946:v 943:= 940:i 934:( 928:} 925:; 916:x 901:{ 898:) 895:v 892:: 889:x 877:( 865:* 859:, 853:* 847:{ 844:= 841:v 835:{ 820:; 811:; 805:* 799:: 793:; 787:= 784:) 772:( 769:= 754:; 748:= 745:) 733:( 727:} 724:; 709:( 694:) 685:n 679:0 673:n 670:( 664:{ 661:) 658:n 652:( 640:} 637:; 628:{ 613:} 610:; 601:{ 592:~ 589:} 586:} 583:; 580:i 577:* 574:= 571:p 568:* 565:{ 562:) 559:p 553:, 550:i 538:. 529:i 520:. 514:= 511:i 505:( 499:; 493:= 490:p 487:* 481:; 472:= 460:. 454:= 448:{ 445:) 442:0 439:( 427:( 421:: 418:) 394:( 388:} 385:; 379:+ 370:{ 358:} 355:; 346:{ 334:; 331:* 325:= 316:: 310:{

Index

object-oriented programming
design pattern
iterator
container
algorithms
"Gang of Four" design patterns

UML
class diagram
UML
sequence diagram

Iterator
C++
pointers
concept
Composite pattern
Container (data structure)
Design pattern (computer science)
Iterator
Observer pattern
Design Patterns: Elements of Reusable Object-Oriented Software
257ff
ISBN
0-201-63361-2
"The Iterator design pattern - Problem, Solution, and Applicability"
Gang Of Four
"The Iterator design pattern - Structure and Collaboration"
ISBN
978-0-321-99278-9

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.

↑