Request help on interface design for data classes


J

Jayden Shui

Hi All,

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!

Jayden
 
Ad

Advertisements

V

Victor Bazarov

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
 
J

Jayden Shui

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

Thank you so much. I'd like the users to use the class Problem to
define their problems (background, sources, receivers, and so on).
Would you please give me an example of hint to how to define such
classes? I really appreciate it.

Best regards,

Jayden
 
V

Victor Bazarov

Thank you so much.

Think nothing of it.
I'd like the users to use the class Problem to
define their problems (background, sources, receivers, and so on).

"Define their problems" is too nebulous to have the meaning clear enough
to start designing a class for it. Be specific. You want the users of
that class to do what with a problem? Give it "a background" and
possibly get the background back from it? Then your "problem" is but a
mere storage for "a background" (whatever that is). Doesn't seem useful
much, to be honest. What would a "Problem" do beyond *storing* the
values for "a background", "sources" and "receivers"? A "Problem"
usually begs to be "solved". Who is going to "solve" it? What is a
"Problem" to do when it has all the information? Does it solve itself?
Does it get passed through some kind of "solver" external to the
problem? Can a "Problem" be solved differently based on some kind of
"precision" or "method" that you could supply to the "Problem"s "solve"
method?

Think about it a bit more.
Would you please give me an example of hint to how to define such
classes? I really appreciate it.

Get a good book on designing OO software. Such books have plenty of
examples. Even a simple C++ book would probably have some examples of
classes and their interfaces and implementations. Except that C++ books
don't usually concentrate on doing the design the right way,
unfortunately. Try "Advanced C++" by James Coplien.

V
 
J

Jorgen Grahn

On 12/8/2011 12:36 PM, Jayden Shui wrote: .... ....

much, to be honest. What would a "Problem" do beyond *storing* the
values for "a background", "sources" and "receivers"? A "Problem"
usually begs to be "solved".

Perhaps the name "Problem" is the problem. I can imagine a design
where "Problem" gets renamed "Sound Landscape" or "Environment for
audio experiment" or "Room", and just describes the (constant) setting
and its physics. Then the "Experiment" could be another object

Experiment(landscape, speed_metal_through_two_speakers);

Perhaps useful if you need to do 1000 isolated experiments in the same
room. Not so useful if you do the same experiment in 1000 different
rooms.

/Jorgen
 
J

Jayden Shui

Think nothing of it.

 > I'd like the users to use the class Problem to


"Define their problems" is too nebulous to have the meaning clear enough
to start designing a class for it.  Be specific.  You want the users of
that class to do what with a problem?  Give it "a background" and
possibly get the background back from it?  Then your "problem" is but a
mere storage for "a background" (whatever that is).  Doesn't seem useful
much, to be honest.  What would a "Problem" do beyond *storing* the
values for "a background", "sources" and "receivers"?  A "Problem"
usually begs to be "solved".  Who is going to "solve" it?  What is a
"Problem" to do when it has all the information?  Does it solve itself?
  Does it get passed through some kind of "solver" external to the
problem?  Can a "Problem" be solved differently based on some kind of
"precision" or "method" that you could supply to the "Problem"s "solve"
method?

Think about it a bit more.


Get a good book on designing OO software.  Such books have plenty of
examples.  Even a simple C++ book would probably have some examples of
classes and their interfaces and implementations.  Except that C++ books
don't usually concentrate on doing the design the right way,
unfortunately.  Try "Advanced C++" by James Coplien.

V

Thank you. I have another class called Solver to solve the problem.
Basically in my software, the two classes: Problem and Solver, are of
the high-level. The class Problem used other lower-level classes such
as Background, Source, and so on. Background, Sources use other lower
classes such as points, directions, and so on. Similar for solver. Do
you feel this logic follow the OOD priciples?

Best regards,

Jayden
 
Ad

Advertisements

J

Jayden Shui

Perhaps the name "Problem" is the problem.  I can imagine a design
where "Problem" gets renamed "Sound Landscape" or "Environment for
audio experiment" or "Room", and just describes the (constant) setting
and its physics.  Then the "Experiment" could be another object

  Experiment(landscape, speed_metal_through_two_speakers);

Perhaps useful if you need to do 1000 isolated experiments in the same
room. Not so useful if you do the same experiment in 1000 different
rooms.

/Jorgen

Yes, choosing another name may be better. How do you think of the ways
to access data or vectors in a class? Thanks.
 
V

Victor Bazarov

[..]
Basically in my software, the two classes: Problem and Solver, are of
the high-level. The class Problem used other lower-level classes such
as Background, Source, and so on. Background, Sources use other lower
classes such as points, directions, and so on. Similar for solver. Do
you feel this logic follow the OOD priciples?

What logic?

V
 
C

Christopher

Hi All,

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!

Jayden



The "problem" with your design is that you are not describing a
"problem" at all, nor a solution.
What you are desribing is a "Domain".

That isn't a bad thing. You always start off by examining the domain
of the problem and creating data structures for it. The next step is
to actually identify what the "problem" is and what algorithms could
be used to "solve" it.

If you are going to have mutliple problems and solutions...or in other
words, functions that act on the data, start off by listing those
functions and what they do, then see what they have in common if
anything.

Don't worry about containing everythin is the domain in one data
structure. When you get to listing things you might do with the data,
you might see that you don't need everything everytime.

For instance, maybe the only requirement of my software is to email an
grand total for the day to the manager.
I can simply run "orders" through a calculator and keep track of the
totals. I wouldn't need to be concerned with people, names, addresses,
phone numbers, states, zip codes, ethninticities, time of day, browser
they used, etc. I just want to know how much money I made.

Without listing some real requirements for your software, no one is
really going to be able to assist you with design.
Start with requirements, then define a domain, then define algorithms.
 
Ad

Advertisements

J

Jorgen Grahn

....

Yes, choosing another name may be better. How do you think of the ways
to access data or vectors in a class? Thanks.

I did not just suggest changing the name, but to also add
"intelligence" or "behavior" to it. If a Landscape contains the
physics, noone outside it needs to know about data or vectors (or
higher abstractions like walls in different materials).

But I don't know anything about the area (sound physics and the maths
involved). I think you need that to make the right choice.

/Jorgen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top