473,805 Members | 2,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Which design in better?

Objects of type X are basic building blocks of my application and
there are a few functions which return XHandle (typedef X** XHandle).
The task at hand needs to use a couple of these functions and then
iterate over X objects.
Because its done frequently and it separates iteration from task-at-
hand'logic we decided to create iterator.

Assuming the availability of functions GetXHandleSize( XHandle) and
DisposeXHandle( XHandle) there are two alternatives.

<b>Option1:</bUsing simple inheritance
class XHandleIterator {
public:
XHandleIterator (void) : mHandle(0L), mCount(0) {}
~XHandleIterato r() { if (mHandle != 0L) DisposeXHandle( mHandle); }

bool Done(void) { return mCount 0; }
// Pre-increment operator
XHandleIterator & operator++(void ) { --mCount; return *this; } // Am
iterating over backwards
// Post-increment operator..
// De-referencing operator
X& operator *(void) { return (*mHandle)[mCount-1]; }
protected:
XHandle mHandle;
int mCount;
};

class XHandleIterator One : public XHandleIterator {
public:
XHandleIterator One(SomeType obj) {
// Creates XHandle someway. Assigns to mHandle and mCount
}
};

class XHandleIterator Two : public XHandleIterator {
XHandleIterator Two(SomeType obj) {
// Creates XHandle in another way. Assigns to mHandle and mCount
}
};

<b>Option2:</bPolicy based design
template<typena me HandleCreator>
class XHandleIterator {
public:
XHandleIterator (SomeType obj) : mHandle(0L), mCount(0)
{
HandleCreator:: Create(obj, &mHandle);
mCount = GetXHandleSize( mHandle)/sizeof(X);
}
~XHandleIterato r() { if (mHandle != 0L) DisposeXHandle( mHandle); }
bool Done(void) { return mCount 0; }
// Pre-increment operator
XHandleIterator & operator++(void ) { --mCount; return *this; }
// Post-increment operator...
// De-referencing operator
X& operator *(void) { return (*mHandle)[mCount-1]; }
private:
XHandle mHandle;
int mCount;
};

struct XHandleCreator1 {
static void Create(SomeType obj, XHandle *handle) {
// Creates XHandle someway. Assigns to handle
}
};

struct XHandleCreator2 {
static void Create(SomeType obj, XHandle *handle) {
// Creates XHandle in another way. Assigns to handle
}
};

typedef XHandleIterator <XHandleCreator 1XHandleIterato rOne;
typedef XHandleIterator <XHandleCreator 2XHandleIterato rTwo;

I am confused over which is the right way to do in this case with all
stipulations stated at the top. The main thing I like about 2nd one is
that mHandle and mCount are private.
Please comment on which of the above alternative is better?

Thanks
Nitesh
Aug 25 '08 #1
5 1403
Sam
Nitesh writes:
Objects of type X are basic building blocks of my application and
there are a few functions which return XHandle (typedef X** XHandle).
A pointer to a pointer?
The task at hand needs to use a couple of these functions and then
iterate over X objects.
Because its done frequently and it separates iteration from task-at-
hand'logic we decided to create iterator.
Can you explain, precisely, why you've decided to reinvent the wheel, why
none of the STL containers and iterators do not meet your needs, and what
does your handmade iterator class does that none of the STL iterators do.

std::vector, std::list, std::set, std::map, and a few other STL containers,
all give you perfectly usable iterator objects. Any particular reasons why
none of them meet your needs?
Assuming the availability of functions GetXHandleSize( XHandle) and
DisposeXHandle( XHandle) there are two alternatives.

<b>Option1:</bUsing simple inheritance
Both of your options are difficult to follow. Pick an STL container that
meets your needs, and use the iterator definition that it gives you.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAki zMXkACgkQx9p3GY HlUOKo+ACfSvjC0 BkzZB0lAlUG52z3 S46m
6G0Anjl/3p5AHFB9v0fIUxd jIHBOAjNv
=+5yw
-----END PGP SIGNATURE-----

Aug 25 '08 #2
On Aug 26, 3:26*am, Sam <s...@email-scan.comwrote:
Nitesh writes:
Objects of type X are basic building blocks of my application and
there are a few functions which return XHandle (typedef X** XHandle).

A pointer to a pointer?
The task at hand needs to use a couple of these functions and then
iterate over X objects.
Because its done frequently and it separates iteration from task-at-
hand'logic we decided to create iterator.

Can you explain, precisely, why you've decided to reinvent the wheel, why
none of the STL containers and iterators do not meet your needs, and what
does your handmade iterator class does that none of the STL iterators do.

std::vector, std::list, std::set, std::map, and a few other STL containers,
all give you perfectly usable iterator objects. Any particular reasons why
none of them meet your needs?
Assuming the availability of functions GetXHandleSize( XHandle) and
DisposeXHandle( XHandle) there are two alternatives.
<b>Option1:</bUsing simple inheritance

Both of your options are difficult to follow. Pick an STL container that
meets your needs, and use the iterator definition that it gives you.

*application_pg p-signature_part
< 1KViewDownload
Its a legacy app. And I am making use of existing functions which
return me Handle to objects of type X. i.e. I have a pointer to an
array pointer and indexing that array is enough to obtain an array
element, So this option was chosen instead of first creating an STL
container from the handle.

Do you still suggest to first copy the handle elements to an STL
container (copy is not constly because its just pointer to X) and then
use STL iterators.

The iterator was made simple just to focus on the problem of confusion
over choosing between alternate approaches. i.e. You have to write
your own iterator. So the problem can be restated as:
You have to write the iterator, but the collection over which it
iterates is obtained in different ways. In that case whats the better
way? Using simple inheritance and letting derived classes obtain the
collection OR using HandleCreation policy? Or any other way?

Thanks
Nitesh
Aug 26 '08 #3
Nitesh <ni************ @gmail.comkirju tas:
Objects of type X are basic building blocks of my application and
there are a few functions which return XHandle (typedef X** XHandle).
The task at hand needs to use a couple of these functions and then
iterate over X objects.
Because its done frequently and it separates iteration from task-at-
hand'logic we decided to create iterator.

Assuming the availability of functions GetXHandleSize( XHandle) and
DisposeXHandle( XHandle) there are two alternatives.

<b>Option1:</bUsing simple inheritance
class XHandleIterator {
public:
XHandleIterator (void) : mHandle(0L), mCount(0) {}
~XHandleIterato r() { if (mHandle != 0L) DisposeXHandle( mHandle); }

bool Done(void) { return mCount 0; }
// Pre-increment operator
XHandleIterator & operator++(void ) { --mCount; return *this; } // Am
iterating over backwards
// Post-increment operator..
// De-referencing operator
X& operator *(void) { return (*mHandle)[mCount-1]; }
protected:
XHandle mHandle;
int mCount;
};

class XHandleIterator One : public XHandleIterator {
public:
XHandleIterator One(SomeType obj) {
// Creates XHandle someway. Assigns to mHandle and mCount
}
};

class XHandleIterator Two : public XHandleIterator {
XHandleIterator Two(SomeType obj) {
// Creates XHandle in another way. Assigns to mHandle and mCount
}
};

<b>Option2:</bPolicy based design
template<typena me HandleCreator>
class XHandleIterator {
public:
XHandleIterator (SomeType obj) : mHandle(0L), mCount(0)
{
HandleCreator:: Create(obj, &mHandle);
mCount = GetXHandleSize( mHandle)/sizeof(X);
}
~XHandleIterato r() { if (mHandle != 0L) DisposeXHandle( mHandle); }
bool Done(void) { return mCount 0; }
// Pre-increment operator
XHandleIterator & operator++(void ) { --mCount; return *this; }
// Post-increment operator...
// De-referencing operator
X& operator *(void) { return (*mHandle)[mCount-1]; }
private:
XHandle mHandle;
int mCount;
};

struct XHandleCreator1 {
static void Create(SomeType obj, XHandle *handle) {
// Creates XHandle someway. Assigns to handle
}
};

struct XHandleCreator2 {
static void Create(SomeType obj, XHandle *handle) {
// Creates XHandle in another way. Assigns to handle
}
};

typedef XHandleIterator <XHandleCreator 1XHandleIterato rOne;
typedef XHandleIterator <XHandleCreator 2XHandleIterato rTwo;

I am confused over which is the right way to do in this case with all
stipulations stated at the top. The main thing I like about 2nd one is
that mHandle and mCount are private.
Please comment on which of the above alternative is better?

Thanks
Nitesh
It appears you want the iterator to perform two tasks: iterate over the
array, and dispose the array when done. It would be cleaner to keep these
tasks separate IMO. To follow the STL pattern there should be a
"container" class, from where the iterators can be obtained by begin()
and end() member functions. The fact that the data is actually living
elsewhere does not have any significance. The "container" object will
call Dispose in its destructor.

The usage would go like this:

XContainer my_xvector = ObtainXHandleIn WhatEverWay();

for(XContainer: :iterator p = my_xvector.begi n();
p != my_xvector.end( ); ++p) {

X& xref = *p;
// Do whatever with X

}

I see no need for inheritance or policy-based templates here, based on
the info you provided. The container object should just have a
constructor and/or an assignment operator taking XHandle as a parameter,
regardless of in which way the handle was obtained. Mixing up this class
with different ways to obtain the handle would just convolute the design.
If really needed, one could provide a separate "factory" class for taking
care of obtaining the handle in different ways.

hth
Paavo
Aug 26 '08 #4
On Aug 26, 11:02*am, Paavo Helde <nob...@ebi.eew rote:
Nitesh <niteshchord... @gmail.comkirju tas:
Objects of type X are basic building blocks of my application and
there are a few functions which return XHandle (typedef X** XHandle).
The task at hand needs to use a couple of these functions and then
iterate over X objects.
Because its done frequently and it separates iteration from task-at-
hand'logic we decided to create iterator.
Assuming the availability of functions GetXHandleSize( XHandle) and
DisposeXHandle( XHandle) there are two alternatives.
<b>Option1:</bUsing simple inheritance
class XHandleIterator {
public:
*XHandleIterato r(void) : mHandle(0L), mCount(0) {}
*~XHandleIterat or() { if (mHandle != 0L) DisposeXHandle( mHandle); }
*bool Done(void) { return mCount 0; }
*// Pre-increment operator
*XHandleIterato r& operator++(void ) { --mCount; return *this; } // Am
iterating over backwards
*// Post-increment operator..
*// De-referencing operator
*X& operator *(void) { return (*mHandle)[mCount-1]; }
protected:
*XHandle mHandle;
*int mCount;
};
class XHandleIterator One : public XHandleIterator {
public:
*XHandleIterato rOne(SomeType obj) {
*// Creates XHandle someway. Assigns to mHandle and mCount
}
};
class XHandleIterator Two : public XHandleIterator {
*XHandleIterato rTwo(SomeType obj) {
*// Creates XHandle in another way. Assigns to mHandle and mCount
*}
};
<b>Option2:</bPolicy based design
template<typena me HandleCreator>
class XHandleIterator {
public:
*XHandleIterato r (SomeType obj) : mHandle(0L), mCount(0)
*{
* HandleCreator:: Create(obj, &mHandle);
* mCount = GetXHandleSize( mHandle)/sizeof(X);
*}
*~XHandleIterat or() { if (mHandle != 0L) DisposeXHandle( mHandle); }
*bool Done(void) { return mCount 0; }
* // Pre-increment operator
*XHandleIterato r& operator++(void ) { --mCount; return *this; }
*// Post-increment operator...
*// De-referencing operator
*X& operator *(void) { return (*mHandle)[mCount-1]; }
private:
*XHandle mHandle;
*int mCount;
};
struct XHandleCreator1 {
* * *static void Create(SomeType obj, XHandle *handle) {
* * * * *// Creates XHandle someway. Assigns to handle
* * *}
};
struct XHandleCreator2 {
* * *static void Create(SomeType obj, XHandle *handle) {
* * * * *// Creates XHandle in another way. Assigns to handle
* * *}
};
typedef XHandleIterator <XHandleCreator 1XHandleIterato rOne;
typedef XHandleIterator <XHandleCreator 2XHandleIterato rTwo;
I am confused over which is the right way to do in this case with all
stipulations stated at the top. The main thing I like about 2nd one is
that mHandle and mCount are private.
Please comment on which of the above alternative is better?
Thanks
Nitesh

It appears you want the iterator to perform two tasks: iterate over the
array, and dispose the array when done. It would be cleaner to keep these
tasks separate IMO. To follow the STL pattern there should be a
"container" class, from where the iterators can be obtained by begin()
and end() member functions. The fact that the data is actually living
elsewhere does not have any significance. The "container" object will
call Dispose in its destructor.

The usage would go like this:

XContainer my_xvector = ObtainXHandleIn WhatEverWay();

for(XContainer: :iterator p = my_xvector.begi n();
* * * * p != my_xvector.end( ); ++p) {

* * * * X& xref = *p;
* * * * // Do whatever with X

}

I see no need for inheritance or policy-based templates here, based on
the info you provided. The container object should just have a
constructor and/or an assignment operator taking XHandle as a parameter,
regardless of in which way the handle was obtained. Mixing up this class
with different ways to obtain the handle would just convolute the design.
If really needed, one could provide a separate "factory" class for taking
care of obtaining the handle in different ways.

hth
Paavo
This is thing I need to do
1) Obtain handle
2) Iterate over the handle
3) Dispose the obtained handle

For my task its the 1st (obtaining handle) that differs. 2nd and 3rd
(iterating over and disposing) are always done in same way so I put
them in same class.

But with your explanation it seems reasonable not to mix 2nd and 3rd.
So, obtaining and disposing handle should be responsibility of one
class and iterating over that should be done by another but related
class.

How about having one XContainer base class that has a nested public
Iterator class that knows how to iterate over the XContainer? And
there will be derived classes OR template specializations
XContainerOne/Two that know how to obtain the handle. Again which of
these two is better?

Thanks
Nitesh
Aug 26 '08 #5
Nitesh <ni************ @gmail.comkirju tas:

[...]
>
This is thing I need to do
1) Obtain handle
2) Iterate over the handle
3) Dispose the obtained handle

For my task its the 1st (obtaining handle) that differs. 2nd and 3rd
(iterating over and disposing) are always done in same way so I put
them in same class.

But with your explanation it seems reasonable not to mix 2nd and 3rd.
So, obtaining and disposing handle should be responsibility of one
class and iterating over that should be done by another but related
class.

How about having one XContainer base class that has a nested public
Iterator class that knows how to iterate over the XContainer? And
That's OK. But I would suggest to name the inner class "iterator" so
there might be a potential chance to pass XContainer to potential
template functions expecting STL containers.
there will be derived classes OR template specializations
XContainerOne/Two that know how to obtain the handle. Again which of
these two is better?
What I cannot understand is why is it necessary to make the handle
obtaining to be part of this system. This would reduce modularity. For
example, if somebody wants to use your classes for iterating over the
handle which has been obtained by some third way, he would have to write
a derived class or template policy for doing this. I don't see why this
should be necessary. KISS!

The situation would be different if the iteration or handle disposal
would be different for handles obtained in different ways. This
effectively means that there would be different kind of handles. In that
case one should use derivation and virtual functions somewhere if the
actual handle type is not known at compile time, and templates/policies
if the actual handle type is always known at compile time.

hth
Paavo

Aug 26 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1958
by: sayoyo | last post by:
Hi Guys, I have to implement a design in Java which is a collection class gathers several units of the same class. I was thinking about two ways to implement it. the first is to create two class: the collection class and the unit class. The second one is to create only one unit class but, the all units(whole collection) are stored into the class variable (static) of the unit class.
133
13291
by: Jane Withnolastname | last post by:
I have a web page that uses an unordered list (<UL>) and the LH (list header) tag. I know LH is a valid tag because it is clearly defined by the W3C here: http://www.w3.org/MarkUp/html3/bulletlists.html The problem is, when I try to validate the page at W3C, it tells me: " element "LH" undefined " My page is 4.01 Transitional, using charset windows-1252. Any ideas why this won't validate?
10
2059
by: heromull | last post by:
We have an asp.net app with about 200 data entry forms. Customers may enter data into any number of forms. Each form's data is persisted in a corresponding sql table. When data entry is complete, it needs to be processed. Here's where the questions start. How can we easily determine in which tables a customer has data and how best to select that data? We're not opposed to putting all the data in a single table. This table would...
1
1538
by: Jason | last post by:
Hi all, I have 1 table which need to store 7 days data in a row, I not sure which table design will gain better performance in SELECT statement. Desgin 1. 7 columns (day1, ..., day7) with char(4) or Desgin 2. 1 column (day_1_to_7) with char(28); to retrieve the data, use substr function Plz advise. Thanks!
105
5368
by: Christoph Zwerschke | last post by:
Sometimes I find myself stumbling over Python issues which have to do with what I perceive as a lack of orthogonality. For instance, I just wanted to use the index() method on a tuple which does not work. It only works on lists and strings, for no obvious reason. Why not on all sequence types? Or, another example, the index() method has start and end parameters for lists and strings. The count() method also has start and end parameters...
4
9008
by: Ronald S. Cook | last post by:
I'm implementing .NET Remoting on a project. We have about 12 instances of the application running at one time, 10 in trucks out in a wireless enviroment (trucks do not leave the yard.. they are feeding cattle), and 2+ on office PCs inside. The app makes calls like, give me all the data on the pen I'm about to feed. While I've seen the list of differences between SingleCall and Singleton, I'm not sure which to employ. Can anyone...
8
2362
by: =?Utf-8?B?eWRibg==?= | last post by:
I need to write a program validate a text file in CSV format. So I will have a class DataType and a lot of of derived class for various type, e.g. IntType, StringType, FloatType, MoneyType, ... etc. For each column of a type, it may accept null/empty value. or not. It may have various max length for StringType, IntType,... etc.
20
3102
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in like 65% of the time of the second routine. Yet both do the same thing. However, the second one seems better in terms of the way the code is written since it helps encapsulate the transformation in the inner loop better making it easier to read,...
3
6710
by: Ty Oft | last post by:
Hello Bytes! Maybe my question seems a bit silly (as one could simply answer it "Well, what are you more passionate about?" or stuff like that - please don't answer like this), but I am in a bit of a dilemma and time is short at the moment and I need to make a decision before it's too late. I have just begun my second year in a UK uni for a BSc Hons Web Systems Development but I am not that happy about the course. Very diluted in my opinion....
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.