By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

Help, about the order requirement of the class functions.

P: n/a
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.

Sep 15 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
shuisheng <sh*********@yahoo.comwrote:
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Sep 15 '06 #2

P: n/a

shuisheng wrote:
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?

Sep 15 '06 #3

P: n/a
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 写道:
shuisheng wrote:
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?
Sep 15 '06 #4

P: n/a
shuisheng wrote:
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....f123421b73f0c).
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
Sep 15 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.