Unformatted text preview: erators (easy in Smalltalk, not as easy in C++) 83 Possibly, even the aggregate (e.g., by maintaining and updating a cursor...) In this case, iterator object only holds state of iteration (but, is this really useful?) Also in this case, client would invoke iteration operations of aggregate with iterator as an argument 84 Iterator pattern Implementation issues (cont d) 3. Robustness of an iterator Pattern Iterator pattern Implementation issues (cont d) 4. The interface of Iterator class Pattern What if iterators can modify aggregate? This may adversely affect iterators working in parallel with each other Not an easy issue: A case of the readers-and-writers problem Trivial solution: Copy aggregate (too expensive? Consistency among copies?) Robust iterator: One that does not interfere with other iterators Usually, aggregate must update the state of iterators after insertion or removal of elements (difGicult to implement?) 85 Minimally, is should have operations Mirst(), next(), isDone(), and current() Other operations can conceivably be added (e.g., skip(i), at(i), atPut(i,x), previous(), etc.) 86 Pattern Iterator pattern (cont
5. Polymorphic iterators d) Iterator pattern Implementation issues (cont d) 6. Access to private parts of aggregate Problem with external iterators Pattern Implementation issues (cont d) Iterators that work with different kinds of aggregates; they require deferred initialization (e.g., by something like Factory Method pattern) and dynamic allocation (i.e., to match lifetime of corresponding aggregate) Handle with care: Memory allocations and deallocations are responsibility of programmer in C++ (put wrapper on stack?) Luckily, Java is garbage-collected 87 C++ solution: DeGine Iterator class as friend of aggregate class; deGine Iterator protocol in protected portion of the class to avoid changing aggregate class interface (friend declarations) each time a new concrete iterator is added Not an easy problem in Smalltalk; often requires adding access protocol to aggregate class (violating information hiding) or using internal iterators (easier to do in Smalltalk) 88 Iterator pattern (cont d) Pattern Visitor pattern Name Visitor Applicability Pattern Implementation issues (cont d) 7. Iteration...
View Full Document
- Spring '12
- Object-Oriented Programming, Design pattern, Subroutine