|
From: Robert Martin on 2 Apr 2008 23:54 On 2008-03-19 11:25:47 -0500, ManicQin <ManicQin(a)gmail.com> said: > Hi, consider the next fundamental class hierarchy > > parseData recieves an array of chars and resolves the data into > commands. > Every device has it's own parseData function (fundamental) > > class baseDevice > { > public: > virtual int parseData(BYTE* bData) = 0; > }; > > class deviceA : public baseDevice > { > public: > int parseData(BYTE* bData) > { return 0; } > } > > class deviceB : public baseDevice > { > public: > int parseData(BYTE* bData) > { return 1; } > } > > You got the point... (Keep in mind that every device has more than one > function) > Now lets say I'm building a separated logger that logs all > communication and prints the RAW data. And I want the logger to be > able to parse the communication into the commands according to the > device's specific parser. I would'nt want to give the parser the > entire device class only the parsing ability... > I thought about encapsulating the parsing logic into a functor and the > logger could just instantiate the parser he needs.. > > 1) Are there any Pit falls in the solution? > 2) Is there any better way to solve this problem? The logger asks the device for the parser: class baseDevice { public: virtual int parseData(BYTE* bData) = 0; virtual Parser* getParser() const = 0; }; The logger then parses the data with that particular parser. The parser probably has a toString method or a generateLogString command or something like that. Each parser returns a loggable string. class Logger { public void logCommand(baseDevice* device, BYTE* bData) { //forgive the inline function Parser* p = device->getParser(); string message = p.generateLogString(bData); logMessage(message); } }; -- Robert C. Martin (Uncle Bob)��| email: unclebob(a)objectmentor.com Object Mentor Inc.� � � � � ��| blog:��www.butunclebob.com The Agile Transition Experts��| web:���www.objectmentor.com 800-338-6716� � � � � � � � ��|
|
Pages: 1 Prev: Identifying C++ Component Dependencies Next: Aspect oriented programming |