473,324 Members | 1,678 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,324 software developers and data experts.

A traversal problem

Hello

I have a design problem, which I don't seem to have a good solution. By
example:

class B;
class C;

class D
{
public:
// Something
};

class A
{
public:
void clear();
int numberOfBs() const;
private:
map<integer, B*> data_;
};

class B
{
public:
void clear();
int numberOfCs() const;
private:
map<integer, C*> data_;
};

class C
{
public:
void clear();
int numberOfDs() const;
private:
vector<D> data_;
};

The similar interfaces is just a coincidence. As you can see the classes
form a hierarchy. This hierarchy is only finite, that is, I am not after
something like composite pattern.

The class hierarchy is made to partition the D data into chunks which
are more conveniently used in the program.

Now, the data in the Ds is the one that should be modifiable by a user.
The user should not be able to use the non-const functions in B and C
(such as clear). The user should only be able to modify the data in Ds
and use the const functions.

It is like the interfaces should be partitioned into two: one for the
"administrator" and one for the "user". On the other hand, I think
iterators must be used in any case, but how do you satisfy the non-const
requirements?

I hope this description is not too vague.

So how do you traverse all D's in all C's in all B's in all A's with
these requirements?

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 5 '06 #1
3 1582
* Kaba:
So how do you traverse all D's in all C's in all B's in all A's with
these requirements?


The requirements seem to be that C, B and A should be non-modifyable
from the client code.

Then expose interfaces, not C, B and A themselves.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 5 '06 #2
Kaba wrote:
I have a design problem, which I don't seem to have a good solution.
By example:

class B;
class C;

class D
{
public:
// Something
};

class A
{
public:
void clear();
int numberOfBs() const;
private:
map<integer, B*> data_;
};

class B
{
public:
void clear();
int numberOfCs() const;
private:
map<integer, C*> data_;
};

class C
{
public:
void clear();
int numberOfDs() const;
private:
vector<D> data_;
};

The similar interfaces is just a coincidence. As you can see the
classes form a hierarchy. This hierarchy is only finite, that is, I
am not after something like composite pattern.

The class hierarchy is made to partition the D data into chunks which
are more conveniently used in the program.

Now, the data in the Ds is the one that should be modifiable by a
user. The user should not be able to use the non-const functions in B
and C (such as clear). The user should only be able to modify the
data in Ds and use the const functions.
So, why aren't those functions that the user isn't supposed to use
declared 'private' then?
It is like the interfaces should be partitioned into two: one for the
"administrator" and one for the "user".
There are several solutions for this. You can declare the 'adminstrator'
a friend of those classes. You can provide a derived class for all the
administrative needs and keep the administration interface 'protected'...
On the other hand, I think
iterators must be used in any case, but how do you satisfy the
non-const requirements?
Make your iterators 'const' up until when they dereference to 'D&'.
I hope this description is not too vague.
Uh.. Maybe just a bit.
So how do you traverse all D's in all C's in all B's in all A's with
these requirements?


I would probably write a bunch of 'for_each' member functions.

You didn't specify what your user gets to work with. What does he/she
have? An instance of 'A'?

V
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #3
This is to both Alf and Victor. Thanks for the replies.

Alf: A is modifyable, while B and C accessed through A are not. The user
is given A. I didn't quite get your answer of exposing interfaces.. Did
you mean to encapsulate B and C totally inside A?
Now, the data in the Ds is the one that should be modifiable by a
user. The user should not be able to use the non-const functions in B
and C (such as clear). The user should only be able to modify the
data in Ds and use the const functions.
So, why aren't those functions that the user isn't supposed to use
declared 'private' then?


It is because the classes themselves, each A, B, C and D should also be
useable by themselves.
It is like the interfaces should be partitioned into two: one for the
"administrator" and one for the "user".


There are several solutions for this. You can declare the 'adminstrator'
a friend of those classes. You can provide a derived class for all the
administrative needs and keep the administration interface 'protected'...


Yes, I also thought of this.. But then I would have to lose the property
that the classes were usable by themselves..
On the other hand, I think
iterators must be used in any case, but how do you satisfy the
non-const requirements?


Make your iterators 'const' up until when they dereference to 'D&'.


This protects the pointers from modification, but when you get the
pointer to B, for example, you are again free to modify it as you wish.
On the other hand, if in some way during the chain from A to D the
object is const, then D is also and can't be modified.
I hope this description is not too vague.
Uh.. Maybe just a bit.


Probably a lot, I am sorry:)
I would probably write a bunch of 'for_each' member functions.

You didn't specify what your user gets to work with. What does he/she
have? An instance of 'A'?


Yes, the user gets the A.

Now, I know the problem description is not enough. So I try again with
my actual application.

I am programming a music composing programming. The top class A is
called Tune. This contains the instruments and notes etc.

The note information in Tune (A) is a collection of Patterns.
A Pattern (B) is a collection of Channels.
A Channel (C) is a collection notes (D).

A Channel is similar to a staff. A Pattern is a collection of parallel
staffs (for example, piano and bass can play at the same time) of some
finite time, kind of blocks. The Tune is then a collection of these
blocks.

The time in a Channel is discretely divided into pieces. The user (well,
a programmer using the api), should be able to directly write and read
these time pieces (write note there, clear that note, etc..).

But the user should also have access to the larger pieces, such as
Patterns, so he can read something useful from it. As the Patterns and
Channels are organized by Tune, the user should not have modification
access to them.

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 6 '06 #4

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

Similar topics

16
by: Tim Tyler | last post by:
Today's: "Directory Traversal Vulnerability": - http://secunia.com/advisories/10955/ More evidence tht PHP was hacked together rapidly without a great deal of thought being given to security....
2
by: ravi mannan | last post by:
Hello all, I'm trying to read an xml file and create a nested JPopupMenu from that. The first thing I want to do is to read in the xml file and put it in a Document using DOM and then do a...
1
by: guy001 | last post by:
Hi, I'm trying to traverse the DOM in a bit of a non-traditional manner and am struggling to get my head around it. Just say i have some elements like so: A |-B |-C | |-D |
2
by: FrankEBailey | last post by:
I've been reading up on Modified Preorder Tree Traversal and it's definitely ideal for the kind of tree structures I need to model for my company's multi-level sales hierarchy. I've implemented the...
1
by: Patient Guy | last post by:
I am trying to write functions that manipulate objects representing dynamic tables in HTML documents in a more friendly way (to me). I do this by creating a table object (tableObject) through a...
2
by: xandra | last post by:
i have 2 question in breadth-first traversal.your help will be appreciated. 1. what is the purpose of the queue in breath-first traversal? 2 suppose you had a function call displayAtDepthN which...
6
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
0
by: Ravi Kumar | last post by:
hi :) I was trying to develop a custom mod_python based web-site, just today. the problem I got though i liked the mod_python's feature of mapping and calling functions in python script by...
6
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree...
2
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.