473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help, about the order requirement of the class functions.

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
4 1370
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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: J. Campbell | last post by:
I'm comfortable with arrays from previous programming, and understand the advantages of c++ vectors...I just don't understand how to use them :~( Can you help me to use a vector<string> in the...
4
by: Jim Langston | last post by:
I understand the rule of three, that if I have a custom constructor, copy or destructor I probably need the other 2. My class object definately has a custom constructor and destructor, but I'm...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
7
by: trialproduct2004 | last post by:
Hi all I am having application which has three classes. in one class say class1, i am having arraylist where i am adding and removing string. there is one method in class1 which is creating...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
10
by: ddtbhai | last post by:
Hello folks, Just wanted to know if there are some 'standard' approaches to enforcing an order in the invocation of calling functions. It is usually needed when initializing some object. e.g...
23
by: casper christensen | last post by:
Hi I run a directory, where programs are listed based on the number of clicks they have recieved. The program with most clicks are placed on top and so on. Now I would like people to be apple to...
9
by: Ramashish Baranwal | last post by:
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in...
16
by: gnawz | last post by:
I have a pagination function I am using in a file called functions.php as below<? //Pagination functions function getPagingQuery($sql, $itemPerPage = 10) { if (isset($_GET) && (int)$_GET > 0) ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.