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

Composite class problem

Hi

I'm making a 3D Engine which consists of the class C3DEngine. Part of this
engine is a file loader, a class called CMeshLoader.
I have made an instance of CMeshLoader in C3DEngine, ie composition.
Unfortunately, CMeshLoader sometimes needs to refer
to private members of C3DEngine. Inheritance, friends, and composition are
all swirling around in my head at the moment. I know that
inheritance is not the solution. Can someone please suggest an elegant way
of solving this problem?

Cheers
dave :)
Jul 22 '05 #1
17 2440

"Dave" <da**********************************@hotmail.co m> a écrit dans le
message de news: 3f******@dnews.tpgi.com.au...
Hi

I'm making a 3D Engine which consists of the class C3DEngine. Part of this
engine is a file loader, a class called CMeshLoader.
I have made an instance of CMeshLoader in C3DEngine, ie composition.
Unfortunately, CMeshLoader sometimes needs to refer
to private members of C3DEngine. Inheritance, friends, and composition are
all swirling around in my head at the moment. I know that
inheritance is not the solution. Can someone please suggest an elegant way
of solving this problem?

Cheers
dave :)


in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
anyway, a class using a second one which needs data from the first, doesn't
seem really right for me.... ? maybe you should wonder about the conception
and WHY does your mesh loader need data from the engine, and maybe there's
some way to externalize or refactor parts of code to make it more intuitive.
Jul 22 '05 #2
"Dave" <da**********************************@hotmail.co m> wrote in message
news:3f******@dnews.tpgi.com.au...
Hi

I'm making a 3D Engine which consists of the class C3DEngine. Part of this
engine is a file loader, a class called CMeshLoader.
I have made an instance of CMeshLoader in C3DEngine, ie composition.
Unfortunately, CMeshLoader sometimes needs to refer
to private members of C3DEngine. Inheritance, friends, and composition are
all swirling around in my head at the moment. I know that
inheritance is not the solution. Can someone please suggest an elegant way
of solving this problem?

Cheers
dave :)


Dave-

I have to agree with the Jolly Green Giant (Le Geant Vert) on this one.
Partitioning the program into input, processing, and output was how we wrote
Fortran programs back in the '60's. With object oriented programming an
object should take care of its own initialization. In your case this
indicates that C3DEngine should have a method, not a separate class, for
reading the input data. If you need a plugin to deal with different data
formats you can try templates or polymorphism. You might want to wade
through the Design Patterns book for ideas. Good luck.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3

"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...
[SNIP] >


in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
anyway, a class using a second one which needs data from the first,

doesn't seem really right for me.... ? maybe you should wonder about the conception and WHY does your mesh loader need data from the engine, and maybe there's
some way to externalize or refactor parts of code to make it more intuitive.


Well, there are certainly cases when it is absolutely fine that a
class/object that is included in a first one needs data from that. Imagine
for example a CUniverse class the encloses a CPlanet object. It's absolutely
fine that for example that CPlanet object obtains the current time from
CUniverse during the simulation of a 3 body problem. In principle I also
think that supplying access functions is the best idea.

Regards
Chris
Jul 22 '05 #4

"Chris Theis" <Ch*************@nospam.cern.ch> a écrit dans le message de
news: bq**********@sunnews.cern.ch...

"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...
[SNIP] >


in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
anyway, a class using a second one which needs data from the first,

doesn't
seem really right for me.... ? maybe you should wonder about the

conception
and WHY does your mesh loader need data from the engine, and maybe there's some way to externalize or refactor parts of code to make it more

intuitive.


Well, there are certainly cases when it is absolutely fine that a
class/object that is included in a first one needs data from that. Imagine
for example a CUniverse class the encloses a CPlanet object. It's

absolutely fine that for example that CPlanet object obtains the current time from
CUniverse during the simulation of a 3 body problem. In principle I also
think that supplying access functions is the best idea.

Regards
Chris


yep, there's always a lot of solutions to a single problem, nevertheless, I
usually have the feeling that when I have a "dual dependancy" (a class
containing another using, the last requiring data from the first), there
must be some way to improve the code design ; without criticizing, this is
my point of view. For example, with your CUniverse and CPlanet, maybe I'll
had some kinda "synchronize()" method to the CUniverse in order to dispath
the time information to all the planets, if the problem allows it. This may
have exactly the same results depending on the context, but this way, the
information are going from the top the bottom of the hierarchy without
needing "dual exchanges".
but of course, I'm pretty sure we can easily find examples where we do have
to go the way you're explaining :)
Jul 22 '05 #5
Hmmm sounds like I've got some restructuring to do. Thanks for all your
help!
Jul 22 '05 #6
"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...
in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?


That defeats the purpose of making them private, and IMO isn't any improvement on reading and
writing the private members directly.

I don't see anything wrong with having one class accessing another's private members as a friend
as long as the accessing class is considered to 'belong' to the class it's accessing. Think of
the two classes as a pair that go together.

However, I'm not sure about the C3DEngine having a CMeshLoader as a member object. File loading
doesn't sound like an inherent responsibility of C3DEngine, which I assume is there to do 3D
stuff. If so, I'd rather separate file loading from it entirely by having the owner of the
C3DEngine create the CMeshLoader object to initialize the C3DEngine as necessary.

DW

Jul 22 '05 #7

"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...

[snip]

| in the class C3DEngine, what about just supplying getters to read the
| private members, and matching setters to write them ?

| anyway, a class using a second one which needs data from the first, doesn't
| seem really right for me.... ? maybe you should wonder about the conception
| and WHY does your mesh loader need data from the engine, and maybe there's
| some way to externalize or refactor parts of code to make it more intuitive.

Composition can sometimes be a better choice than inheritence
though. In the following example, a 'Car' is represented as a
composite class, rather than an inheritence heirarchy.

class Car
{
Engine E;
Transmission T;
public:
Car( const Engine& e, const Transmission& t )
: E( e ), T( t ) {}
};

An 'Engine' is not a 'Car', nor is a 'Transmission', so the
inheritence model doesn't realy make much sense. However,
a 'Car' *Has-A* engine and *Has-A* transmission, and this
is where composition can help.

I admit, that composition can get complex real quickly when
you need to manage communication between your objects :-), but
there are times where it can better model a real world object
imo.

Cheers.
Chris Val
Jul 22 '05 #8
Spot on chris, that's exactly the kind of situation i'm modelling.
Jul 22 '05 #9
David White wrote:
"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...
in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
That defeats the purpose of making them private, and IMO isn't any
improvement on reading and writing the private members directly.


It is an improvement. If you decide for some reason that the
implementation should be changed, you can still do that without
changing the interface. That's the point of making things private.
I don't see anything wrong with having one class accessing another's
private members as a friend as long as the accessing class is
considered to 'belong' to the class it's accessing. Think of the two
classes as a pair that go together.


I agree.

Jul 22 '05 #10
On Sat, 6 Dec 2003 21:25:10 -0800, "Dave"
<da**********************************@hotmail.co m> wrote:
Hi

I'm making a 3D Engine which consists of the class C3DEngine. Part of this
engine is a file loader, a class called CMeshLoader.
I have made an instance of CMeshLoader in C3DEngine, ie composition.
I've been working on a 3D engine myself, on and off for the past 2 yr.
Unfortunately, CMeshLoader sometimes needs to refer
to private members of C3DEngine. Inheritance, friends, and composition are
all swirling around in my head at the moment. I know that
inheritance is not the solution. Can someone please suggest an elegant way
of solving this problem?


I banged my head on the wall over the same thing. Tons of
environmental stuff that you'd think should be members of the
'TopClass'. That's where the whole misconception lies.

You just need a few server classes; some could be singletons, some
not.

You'll probably need a class to represent the camera.
Initially camera could be a singleton; but eventually you'll have a
vector of cameras, as you'll need extra ones for mirror reflections,
etc.

The World class: It doesn't matter that the world 'contains' all
things, in the real world. In the programming world, World is just a
data server.

Camera, World, App, OGLobj, Lighting, these are all classes that have
tons of data members, are seldom modified (once per frame), but are
accessed very often. Tons of get functions to write.

There's no need to put 'stuff' inside World; try NOT to put things
inside other things unless there's a practical reason to. Otherwise
you'll end up having to write,

the_world->continent[2]->country[7]->city[3]...->my_cat->move()

if you know what I mean.
Google up on Extreme Programming, and make it as simple as possible.

Cheers!
Jul 22 '05 #11

"Chris ( Val )" <ch******@bigpond.com.au> a écrit dans le message de news:
bq*************@ID-110726.news.uni-berlin.de...

"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...

[snip]

| in the class C3DEngine, what about just supplying getters to read the
| private members, and matching setters to write them ?

| anyway, a class using a second one which needs data from the first, doesn't | seem really right for me.... ? maybe you should wonder about the conception | and WHY does your mesh loader need data from the engine, and maybe there's | some way to externalize or refactor parts of code to make it more intuitive.
Composition can sometimes be a better choice than inheritence
though. In the following example, a 'Car' is represented as a
composite class, rather than an inheritence heirarchy.


I've never said that inheritence should be prefered to composition (in fact,
I'm thinking almost the opposite, too much inheritence leads to complex
class design that can't be modified easiky when changes or evolutions are
needed... huge inheritence hierachy makes refactoring harder in general, I
think)
What I'm saying is that when composition is used, "dual depency" (A uses a
B, but B needs data from A ....) sounds weird to me... and design should be
reviewed, when possible.

anyway, thx for the "composition howto" :)
Jul 22 '05 #12

"David White" <no@email.provided> a écrit dans le message de news:
ne******************@nasal.pacific.net.au...
"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:bq**********@news.tiscali.fr...
in the class C3DEngine, what about just supplying getters to read the
private members, and matching setters to write them ?
That defeats the purpose of making them private, and IMO isn't any

improvement on reading and writing the private members directly.

I don't see anything wrong with having one class accessing another's private members as a friend as long as the accessing class is considered to 'belong' to the class it's accessing. Think of the two classes as a pair that go together.

However, I'm not sure about the C3DEngine having a CMeshLoader as a member object. File loading doesn't sound like an inherent responsibility of C3DEngine, which I assume is there to do 3D stuff. If so, I'd rather separate file loading from it entirely by having the owner of the C3DEngine create the CMeshLoader object to initialize the C3DEngine as necessary.
DW


same as Rolf.

anyway, if a class is "belonging" to another one, and those two should be
considered as a pair that go together, maybe making an inner-class could
clarify this strong relationship. Thus, as your pointing, the CMeshLoader
shouldn't be aggregated to the C3DEnging.

Maybe I'd make the CMeshLoader a singleton, filling one or more
CMeshLibrary, this lately being used by the C3DEngine... :-?
If we knew WHY the meshloader needs data from the 3D engine, we should
certainly be able to find more appropriate class design...
Jul 22 '05 #13
"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:br**********@news.tiscali.fr...

"David White" <no@email.provided> a écrit dans le message de news:
ne******************@nasal.pacific.net.au...
I don't see anything wrong with having one class accessing another's private members as a friend
as long as the accessing class is considered to 'belong' to the class it's

accessing. Think of
the two classes as a pair that go together.

However, I'm not sure about the C3DEngine having a CMeshLoader as a member

object. File loading
doesn't sound like an inherent responsibility of C3DEngine, which I assume

is there to do 3D
stuff. If so, I'd rather separate file loading from it entirely by having

the owner of the
C3DEngine create the CMeshLoader object to initialize the C3DEngine as

necessary.

DW


same as Rolf.

anyway, if a class is "belonging" to another one, and those two should be
considered as a pair that go together, maybe making an inner-class could
clarify this strong relationship.


For maintenance purposes, they need to be considered a pair, because if one changes the other
will probably need to change as well. However, in another way I'd prefer them to be completely
separate from each other. If C3DEngine is primarily doing 3D stuff, and if it's reasonably
general-purpose, then it's conceivable that some applications would not need the CMeshLoader,
and wouldn't want to have to even compile it. So, I'd want to keep them apart, but at the same
time not forget that if C3DEngine changes - including even its private members - then
CMeshLoader has to change also. I'm not sure how best to achieve both.
Thus, as your pointing, the CMeshLoader
shouldn't be aggregated to the C3DEnging.

Maybe I'd make the CMeshLoader a singleton, filling one or more
CMeshLibrary, this lately being used by the C3DEngine... :-?
If we knew WHY the meshloader needs data from the 3D engine, we should
certainly be able to find more appropriate class design...


My understanding is that CMeshLoader loads the values of C3DEngine's data members and then
assigns those members to the loaded values, so CMeshLoader is a sort of serialization class.

DW

Jul 22 '05 #14
"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:br**********@news.tiscali.fr...

I've never said that inheritence should be prefered to composition (in fact,
I'm thinking almost the opposite, too much inheritence leads to complex
class design that can't be modified easiky when changes or evolutions are
needed... huge inheritence hierachy makes refactoring harder in general, I
think)


Not only that, but wherever you have one object interacting with another, you have an
opportunity to make use of polymorphism. If you use a mixture of inheritance and composition,
then the combination of many objects interacting, where each object can be one of several
different classes, can give an application enormous flexibility in its run-time behaviour.

DW

Jul 22 '05 #15
hmmm well in lieu of major reconstruction, i think at the moment
my best solution is to simply pass a pointer of C3DEngine to CMeshLoader.
Not
to flash, but does the job. Thanks all for your help, its most appreciated.

cheers
dave
Jul 22 '05 #16

"Le Géant Vert" <_N*****************@tiscali.fr> wrote in message
news:br**********@news.tiscali.fr...
|
| "Chris ( Val )" <ch******@bigpond.com.au> a écrit dans le message de news:
| bq*************@ID-110726.news.uni-berlin.de...

[snip]

| > Composition can sometimes be a better choice than inheritence
| > though. In the following example, a 'Car' is represented as a
| > composite class, rather than an inheritence heirarchy.
| >
|
| I've never said that inheritence should be prefered to composition (in fact,
| I'm thinking almost the opposite, too much inheritence leads to complex

My apologies - I didn't mean to imply that you did
prefer one over the other. It is difficult to get
ones point across, in a textual manner sometimes :-).

| class design that can't be modified easiky when changes or evolutions are
| needed... huge inheritence hierachy makes refactoring harder in general, I
| think)
| What I'm saying is that when composition is used, "dual depency" (A uses a
| B, but B needs data from A ....) sounds weird to me... and design should be
| reviewed, when possible.

Agreed.

| anyway, thx for the "composition howto" :)

Any time :-).

Cheers.
Chris Val
Jul 22 '05 #17

"Dave" <da**********************************@hotmail.co m> wrote in message
news:3f********@dnews.tpgi.com.au...
| hmmm well in lieu of major reconstruction, i think at the moment
| my best solution is to simply pass a pointer of C3DEngine to CMeshLoader.
| Not
| to flash, but does the job. Thanks all for your help, its most appreciated.

Hi Dave.

Since we haven't seen any of your code, it is difficult to
explain away some kind of sample - but since I like samples :-),
here is one that uses member functions to pass the 3DEngine
object around via reference.

It is a *wild rough guess*, but it should present some ideas:

# include <iostream>
# include <fstream>
# include <ostream>

class C3DEngine;

class CMeshLoader
{
private:
std::ifstream InFile;
public:
template<class T>
bool LoadData( const std::string& Filename, const T& Engine )
{
if( Engine.IsReady() ) // Attempt to load data file...
{
InFile.open( Filename.c_str() );

if( !InFile )
return false;
else
return true;
}

return false;
}
};

class C3DEngine
{
private:
std::string FileName;
bool EngineReady;
CMeshLoader Loader;
public:
C3DEngine( const std::string& filename, const bool& state = false )
: FileName( filename ), EngineReady( state ) {}

bool LoadData() { return Loader.LoadData( FileName, *this ); }
bool IsReady() const { return EngineReady; }
};

int main()
{
C3DEngine Engine( "DataFile.txt", true );

if( Engine.LoadData() )
std::cout << "Engine data loaded. " << std::endl;
else
std::cout << "ERROR: Could not load up engine. "
<< std::endl;

return 0;
}

Cheers.
Chris Val
Jul 22 '05 #18

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

Similar topics

0
by: Michael Andersson | last post by:
Given a set of classes class A { enum [ ID = 0x0001} }; class B { enum [ ID = 0x0002} }; class B { enum [ ID = 0x0004} }; I wish to generate a composite class, perhaps using something like...
0
by: AshifToday | last post by:
this was my and my frineds little project in earlier classes, the program seperates the composite and prime numbers in two sections of the screen ===================== /* This program has...
1
by: sleigh | last post by:
Hello, I'm building a web application that will build a dynamic form based upon questions in a database. This form will have several different sections that consist of a panel containing one to...
9
by: Alphonse Giambrone | last post by:
I have built a simple composite control that consists of a textbox, requiredfieldvalidator and rangevalidator. For properties that are unique to the individual control, I set/get them directly...
0
by: AndrewF | last post by:
Hi there. I am royally stuck with this so any help would be greatly appreciated. Basically I have a set of classes that create composite controls for the user. One of these is an upload object...
2
by: pkpatil | last post by:
Hi, Can a private composite object in a class access private or protected members of base class? For e.g. class composite { void memberFunction(); };
14
by: dave.dolan | last post by:
Basically I'd like to implement the composite design pattern with leaves that are either of reference or value types, but even using generics I can't seem to avoid boxing (using ArrayList or...
0
by: Jacques Vandensavel | last post by:
I have a legacy table which is composed out three key fields. It is representing a bank. I'm using NHibernate in my ASP.NET application to glue everything together. I'm already searching for an...
6
by: shapper | last post by:
Hello, I am working in a class library with various custom controls. In which cases should a control inherit Control, WebControl and CompositeControl classes? And when should a custom...
1
by: mathieu | last post by:
Hi there, I am currently stuck on the following simple problem: implementing an iterator for a class using the Composite Pattern. I feel like I have to reimplement a complete new class for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.