p = new person(); b = new book(); p = b->borrower; s = p->books; tf = b->inlibrary(); b->borrow(p); p->allocate(b); p->deallocate(b); b->_return(p);
class book {\fbox{book}
public: person* borrower; book() {} void borrow( person* p ) { borrower = p; } void _return( person* p ) { borrower = 0; } bool inlibrary() { return !borrower; } };
class person {\fbox{person}
public: person() { books = new set(); } void allocate( book* b ) { books->insert(b); } void deallocate( book* b ) { books->remove(b); } set * books; };
book* Stroustrup = new book(); book* ChandyMisra = new book(); book* Smalltalk80 = new book(); person* Hans = new person(); person* Cees = new person(); Stroustrup->borrow(Hans); Hans->allocate(Stroustrup); ChandyMisra->borrow(Cees); Cees->allocate(ChandyMisra); Smalltalk80->borrow(Cees); Cees->allocate(Smalltalk80);
class Event {\fbox{\fbox{Event}}
public: virtual void operator()() = 0; };
class Borrow : public Event {\c{\fbox{Borrow}}
public: Borrow( person* _p, book* _b ) { _b = b; _p = p; } void operator()() { require( _b && _p );\c{// _b and _p exist}
_b->borrow(p); _p->allocate(b); } private: person* _p; book* _b; };
class Return : public Event {\c{\fbox{Return}}
public: Return( person* _p, book* _b ) { _b = b; _p = p; } void operator()() { require( _b && _p ); _b->_return(p); _p->deallocate(b); } private: person* _p; book* _b; };