lecture8 - CSE 335 Dr. Charles B. Owen Object-Oriented...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE 335 Dr. Charles B. Owen Object-Oriented Programming 1 The Iterator Pattern Iterator pattern: Page 257 template <class T> class CIterator { public: virtual void First() = 0; virtual void Next() = 0; virtual bool IsDone() = 0; virtual T Current() = 0; protected: CIterator() {} CIterator(CIterator &) {} }; CSE 335 Dr. Charles B. Owen Object-Oriented Programming 2 This is bad class CCity { public: // ... std::list<CTile *> &GetTiles() {return mTiles;} private: std::list<CTile *> mTiles; }; Why??? list<CTile *> &tiles = mCity->GetTiles(); for(list<CTile *>::iterator t=tiles.begin(); t != tiles.end(); t+ +) { // Do something } CSE 335 Dr. Charles B. Owen Object-Oriented Programming 3 This is bad class CCity { public: // ... std::list<CTile *> &GetTiles() {return mTiles;} private: std::list<CTile *> mTiles; }; All right, Ryan, we just unzipped our fly. Capt. Bart Mancuso, The Hunt for Red October list<CTile *> &tiles = mCity->GetTiles(); for(list<CTile *>::iterator t=tiles.begin(); t != tiles.end(); t+ +) { // Do something } What if CCity has pointers to tiles for other purposes? We can do awful things like: mCity->GetTiles().erase(t); mCity->GetTiles().push_back(new CUglyTile()); CSE 335 Dr. Charles B. Owen Object-Oriented Programming 4 This is bad class CCity { public: // ... std::list<CTile *> &GetTiles() {return mTiles;} private: std::list<CTile *> mTiles; }; But, what if we have clients that need to be able to go through the tiles and check for something or another? list<CTile *> &tiles = mCity->GetTiles(); for(list<CTile *>::iterator t=tiles.begin(); t != tiles.end(); t+ +) { // Do something } CSE 335 Dr. Charles B. Owen Object-Oriented Programming 5 One Alternative Visitor pattern Wonderful if we want to look at everything Avoids issues of down-casting But, sometimes it is not the ideal solution CSE 335 Dr. Charles B. Owen Object-Oriented Programming 6 One Alternative Visitor pattern downsides Fixed traversal order Visits everything, cant stop when done. Often interested in only one level of a tree. Were not going to replace visitor pattern. The new pattern well discuss today often is combined with the visitor pattern. CSE 335 Dr. Charles B. Owen Object-Oriented Programming 7 Suppose Your radio frequency identification (RFID) system has located 10 animals in a field. Your have the National Animal Identification System (NAIS) number for each animal (a 15 digit number). You want to find the objects for these 10 animals among your 500....
View Full Document

This note was uploaded on 12/12/2010 for the course CSE CSE 335 taught by Professor Owen during the Fall '10 term at Michigan State University.

Page1 / 30

lecture8 - CSE 335 Dr. Charles B. Owen Object-Oriented...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online