Question regarding a container of objects

T

tomix

Hi,

assuming i have a container of objects, for example:

Course contains a list of CourseOffer which itself is a container of
duration objects

And assuming CourseOffer only contained by Course

i wonder what do you think is a better way to implement it


Course* c=GetCourse();
CourseOffer* cf=new CourseOffer(.....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);
c->Add(cf);
:
:

or

Course* c=GetCourse();
CourseOffer* cf=c.CreateCourseOffer(....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);

I'll go for the second since course control the creation and lifetime
of a course offer

Thanks in advance.
 
V

Victor Bazarov

tomix said:
assuming i have a container of objects, for example:

Course contains a list of CourseOffer which itself is a container of
duration objects

And assuming CourseOffer only contained by Course

i wonder what do you think is a better way to implement it


Course* c=GetCourse();
CourseOffer* cf=new CourseOffer(.....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);
c->Add(cf);

or

Course* c=GetCourse();
CourseOffer* cf=c.CreateCourseOffer(....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);

I'll go for the second since course control the creation and lifetime
of a course offer

Thanks in advance.

If 'CourseOffer' has to be public, then yes, the latter choice is better.
But if 'CourseOffer' is just an implementation detail of 'Course', then
you could simply add the proper interface to 'Course'...

V
 
D

Daniel T.

tomix said:
Hi,

assuming i have a container of objects, for example:

Course contains a list of CourseOffer which itself is a container of
duration objects

And assuming CourseOffer only contained by Course

i wonder what do you think is a better way to implement it


Course* c=GetCourse();
CourseOffer* cf=new CourseOffer(.....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);
c->Add(cf);
:
:

or

Course* c=GetCourse();
CourseOffer* cf=c.CreateCourseOffer(....);
cf->AddDuration(10,12);
cf->AddDuration(16,20);

I'll go for the second since course control the creation and lifetime
of a course offer

Thanks in advance.

I generally go for either completely private, or generally extensible.
This means either option one above (which allows us to provide
sub-classes of CourseOffer to a Course,) or make it so the user of
Course need know nothing at all about the CourseOffer class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top