September 27th, 2006, 10:17 am
QuoteOriginally posted by: ananihdvPlease post the sample code. I want to see some real life T & P code.I think that one difficulty with books on templates is that the authors jump into the code without doing the analysis/rationale/why business first.Anyway, the concern here is to show how to do policies (traits not yet). I will take 2 patterns Command and Visitor. Assume that we have a context hierarchy of Instruments and corresponding Visitor hierarchy (for greeks, for example). Now the (G)UI can use Command which is an action on a receiver, yes? (see GOF)Now GOF 'classico' says 1 class for each Command but that is not necessary if 1) one uses templates 2) use Visitor as the action(). So the command class <requires> certain functioanlity (accept() and visit()) and <provides> (exec()) to its clients. Then we get reusable code. QEDThi is an example of policy-based design (IN TEMPLATES). There are many more examples...here it is:Quotetemplate <class Receiver, class Action> class GenericCommand{ // The ability to assign arbitrary functionality to arbitrary entities public: enum CmdType {no_change, normal, no_undo}; GenericCommand(CmdType type = no_change) { r = 0; a = 0; ctype = type; } GenericCommand(Receiver& rec, Action& action, CmdType type = no_change) { r = &rec; a = &action; ctype = type; } virtual void Execute() { cout << "executing accept\n"; r -> Accept(*a); } CmdType getType() const { return ctype; } protected: Receiver* r; Action* a; CmdType ctype;};
Last edited by
Cuchulainn on September 26th, 2006, 10:00 pm, edited 1 time in total.