Help, about the order requirement of the class functions.

S

shuisheng

Dear All,

I have a question, maybe simple, but it puzzles me a lot.

Assume I have a class

class CMyClass
{
public:
// Set a
void SetA( );

// Set b
void SetB( );

// Set c
void SetC( );

// Operation A
void OpA( );

// Operation B
void OpB( );

// Operation C
void OpC( );
};

There is some order requirement in using those functions due to the
class design. For the settings, we must first Set A, then B and C. And
similar to the operations. But the users don't know it. How can I make
a good interface or class design to avoid such problem?

Thank you for your help.

Shuisheng.
 
M

Marcus Kwok

shuisheng said:
Dear All,

I have a question, maybe simple, but it puzzles me a lot.

Assume I have a class

class CMyClass
{
public:
// Set a
void SetA( );

// Set b
void SetB( );

// Set c
void SetC( );

// Operation A
void OpA( );

// Operation B
void OpB( );

// Operation C
void OpC( );
};

There is some order requirement in using those functions due to the
class design. For the settings, we must first Set A, then B and C. And
similar to the operations. But the users don't know it. How can I make
a good interface or class design to avoid such problem?

Well, if you need to pass parameters to your "set" functions, like

void SetA(int a);
void SetB(int b);
void SetC(int c);

then maybe I would create a wrapper function,

void Set(int a, int b, int c)
{
SetA(a);
SetB(b);
SetC(c);
}

Similarly for the operations,

void Op()
{
OpA();
OpB();
OpC();
}

at which point make the original SetA(), SetB(), SetC(), OpA(), OpB(),
and OpC() either private or protected.
 
P

Peter

shuisheng said:
Dear All,

I have a question, maybe simple, but it puzzles me a lot.

Assume I have a class

class CMyClass
{
public:
// Set a
void SetA( );

// Set b
void SetB( );

// Set c
void SetC( );

// Operation A
void OpA( );

// Operation B
void OpB( );

// Operation C
void OpC( );
};

There is some order requirement in using those functions due to the
class design. For the settings, we must first Set A, then B and C. And
similar to the operations. But the users don't know it. How can I make
a good interface or class design to avoid such problem?


A good class interface allows all of its public methods to be called at
any time
assuming that a constructor must be called first.
So a class requiring that you call its methods in a certain order is
bad design.
Why prevents you to pass everything what is needed to the constructor?
 
S

shuisheng

To set such as a, b, and c, I need a complicate interface with many
argument. So I split to set them indicidually. Another reason is that
a, b and c are kind of a concept that users are easy to understand. If
I put them together like

void Set(.., ..., .., .., ..., .., .., .., ..., ..., ...)
For a For b For c

I think it is not easy to understand.

Peter 写é“:
 
N

Nate Barney

shuisheng said:
> To set such as a, b, and c, I need a complicate interface with many
> argument. So I split to set them indicidually. Another reason is that
> a, b and c are kind of a concept that users are easy to understand. If
> I put them together like
>
> void Set(.., ..., .., .., ..., .., .., .., ..., ..., ...)
> For a For b For c

This topic was just covered in a thread called "enforcing function
calling order". Google groups:
(http://groups.google.com/group/comp...42954/d9af123421b73f0c?hl=en#d9af123421b73f0c).
Bo Persson's solution of creating separate objects for a, b, and c
seems to be the most elegant way to handle the problem.

Also, please don't top-post.

Nate
 

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

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top