开发者

Cursor vs Iterator pattern [closed]

开发者 https://www.devze.com 2023-03-20 05:40 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

In numerous texts, I've seen the term cursor used interchangeably with the term iterator. However, it doesn't seem right that they are treated as the same thing.

To me, an iterator allows iteration of a container with no knowledge of the container itself. A cursor, on the other hand, allows iteration of a container as well but has implementation details specific to the container type, so it does keep a reference to the container. Additionally a cursor's interface mirrors the interface of the container kind of like the facade pattern.

Here is an example of what I would consider a cursor:

class Book {};

class Library
{
    std::vector<Book> books;

    bool IsBookHardCover( int bookIndex );
    bool IsBookSoftCover( int bookIndex );

    BookCursor GetFirstBook();
};

class BookCursor
{
    std::vector<Book>& books;
    int currentBook;

 开发者_JAVA百科   bool IsHardCover();
    bool IsSoftCover();

    void Next();
};

So basically I make the distinction between iterators and cursors based on their dependency or knowledge of the container they iterate. Is this an appropriate distinction? If not, what would you consider the design pattern I've outlined in my code example above?

Note that my code example above should be treated as pseudocode since I did not compile it and it also lacks the constructors required.


The Cursor pattern you described is the combination of two patterns: Proxy and Iterator. The reason the standard library is different is to avoid needlessly coupling those two behaviors.


Wikipedia redirects Cursor Pattern to the page for Iterator Pattern. With that in mind, I'd say you're splitting hairs. If we may consider wikipedia to be a definitive reference, the two terms can be used interchangeably.


Iterators (at least the way the are defined in the C++ standard) don't give you access to the properties of the underlying container but those details are accessible to you through the different iterator categories (which are accessible through iterator_tags).

I also think your example is flawed. Why would a collection of data mirror the interface of the data (e.g. IsHardCover()) when I can access the contained value and query itself for its properties?


Traditionally long before the term design pattern was used, cursers where more powerfull than iterators as they allowed to a bigger extend to modify the underlying datastructure. Like insertBefore(), insertAfter() or insertAt() etc. Some implementations allowed it that multiple cursers could operate on the same container and they informed each other about manipulations (they are called stable iterators in our days, or stable data structures)

0

精彩评论

暂无评论...
验证码 换一张
取 消