The class choice will pass data it receives to one of two datahandlers. The interface looks as follows:
interface choice : public datahandler { protected: choice(); public: choice(bpgenerator* g, double p); // constructor void next(datahandler* dh1, datahandler* dh2); // set next handlers datahandler* next(); // get 1 of the 2 handlers int makechoice(); // return OK or FALSE };
The member next chooses one of the two possible next handlers based on the result of makechoice. If makechoice returns OK, next will return datahandler 1, otherwise it will return datahandler 2. The member makechoice returns OK with a chance specified by means of the generator and the parameter p, where p represents the chance that a data object is passed to the first datahandler.
When one wants to base the decision on something else than pure chance, one should override makechoice. Say we have two kinds of data objects, simple and complex. We could derive a new class from data, say complexdata, with a member function returning the complexity:
int complex(); // OK if complex, FALSE otherwise
Now we can derive a new class from choice, say complex_choice, that looks as follows:
interface complex_choice : public choice { private: data* current_data; public: complex_choice(); void pass(data* d); int makechoice(); };
The constructor looks as follows:
complex_choice::complex_choice() : choice() { }We implement its member makechoice as follows:
int complex_choice::makechoice() { return (((complexdata*)current_data) -> complex()); }
And we override the member pass to set the new local variable current_data:
void complex_choice::pass(data* d) { current_data = d; choice::pass(d); }
So, when complex_choice has to pass a complex data object, makechoice will return OK so next will return the first datahandler. If the data object is simple, makechoice returns FALSE and the second datahandler will receive the data.