Command, Iterator, and Visitor Patterns in the Lexi Document Editor

This section of the GoF case study addresses three design challenges in the Lexi editor: decoupling user operations from UI widgets (Command pattern), traversing heterogeneous glyph structures without exposing internal data structures (Iterator pattern), and performing diverse analyses on glyph structures without polluting the Glyph interface (Visitor pattern via double dispatch).

Key Concepts

Commands and Syntax

`cpp

class Command {

public:

virtual void Execute() = 0;

virtual void Unexecute();

virtual bool Reversible();

};

`

`cpp

class Iterator<T> {

virtual void First() = 0;

virtual void Next() = 0;

virtual bool IsDone() = 0;

virtual T CurrentItem() = 0;

};

`

`cpp

Iterator<Glyph*>* i = g->CreateIterator();

for (i->First(); !i->IsDone(); i->Next()) {

Glyph* child = i->CurrentItem();

}

`

`cpp

Iterator<Glyph*>* Row::CreateIterator() {

return new ListIterator<Glyph*>(_children);

}

`

`cpp

// In each Glyph subclass:

void Character::CheckMe(SpellingChecker& checker) {

checker.CheckCharacter(this);

}

`

Relationships

Exam-Relevant Points