Re: Request help on interface design for data classes
On 12/8/2011 11:28 AM, Jayden Shui wrote:
I have classes which primarily collect and provide data information.
For example, the class Problem is used to define a physical problem to
be modeled. The problem has background (such as air), sources (such as
speakers at some positions), receivers (such as microphones at some
positions), and objects (such as walls, tables and chairs). I'd like
to compute the sound heard or recorded at the receivers.
My questions is how to build a good code for the class Problem? I have
a number of similar classes. I figured out several versions. Please
help me compare, give me your comments and suggestion, and your better
design for the class Problem?
// -------------- Version 1: detail all the interface functions.
----------------
class Problem
{
public:
void SetBackground();
Background const& GeBackground() const;
void AddSource(Souce const& source);
std::vector<Source> const& GetSourceVector() const;
private:
Background mBg;
std::vector<Source> mSrcVector;
};
// --------- Version 2: use macro to hide the details and code is
small --------------
#define ACCESSOR(Type, Name) ...
#define VECTOR_ACCESSOR(Type, Name) ...
class Problem
{
ACCESSOR(Background, Background);
VECTOR_ACCESSOR(Source, Source);
};
// --------- Version 3: make the attribute reference public for users
------------
class Problem
{
public:
Background& BackgroundR();
std::vector<Source>& SourceVectorR();
private:
Background mBg;
std::vector<Source> mSrcVector;
};
I appreciate your help!
You're approaching your problem in the wrong way. Classes are designed
by what they do first (interfaces), and only then (based on what they
do) what they contain (implementation).
What does your "Problem" do? What do you expect to ask it? How do you
initialize it? Does it keep a state at all?
The design of a class has to be done based on the code that *uses* that
class, not in a vacuum.
V
--
I do not respond to top-posted replies, please don't ask