Pure Interfaces
Hi,
I had a question to ask about inheritance and using pure interfaces as
opposed to abstract classes that have data members. An example would
be as follows. In the financial industry all options have expiration,
callability/puttability. Now this is seems to be a common malaise as I
see it in the design and implementation of the class heirarchy.
class OptionBase
{
protected:
date expdate_;
call_put optype_;
public:
virtual ~OptionBase(){};
virtual bool setattribute(const std::string& name, double v) = 0;
virtual bool getattribute(const std::string& name, double& v) = 0;
virtual bool setattribute(const std::string& name, int v) = 0
---------------------------
/* Have all your getters and setters */
};
class VanillaOption : public OptionBase
{
private:
exercisetype extype_;
settlementtype settype_;
/* other membes */
public:
virtual bool setattribute(const std::string& name, double v) ;
virtual bool getattribute(const std::string& name, double&
v) ;
virtual bool setattribute(const std::string& name, int v) ;
};
In the situation where your heirarchy is just two classes deep it
seems not so bad to have the expiration date and the call/put option
to be in the OptionBase class. If the heirarchy gets any deeper, it
makes the code a bit unreadable as one needs to look at 2-3 different
header files before one gets an idea as to where infromation is.
What is the recomended solution or guideline if any? I usually
separate the data into a structure and use a pure interface and use
composition to reuse common shared data. I find it easier to read and
understand.
i.e:
struct OptionBaseData
{
date expdate_;
call_put optype_;
};
struct OptionBase
{
virtual ~OptionBase(){};
virtual bool setattribute(const std::string& name, double v) = 0;
virtual bool getattribute(const std::string& name, double& v) = 0;
virtual bool setattribute(const std::string& name, int v) = 0
---------------------------
/* Have all your getters and setters */
};
class VanillaOption : public OptionBase
{
private:
OptionBaseData basedata_;
exercisetype extype_;
settlementtype settype_;
/* other members */
public:
virtual bool setattribute(const std::string& name, double v) ;
virtual bool getattribute(const std::string& name, double&
v) ;
virtual bool setattribute(const std::string& name, int v) ;
-----------------------------------
/* Other getters and setters */
};
Regards,
RK
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]