Reputation: 3423
I have classes with many properties, which consists, in some cases, other complex classes. Not allways I'm going to use this complete schemas, but sometimes I'm doing so. What's the best practice to create an object, deal with it and then retrieve information on demand?
I'm giving an very simple example to clarify:
Supose a class student
which is related to a class professor
. This class is related to other classes like classes
, schedules
, etc. Not allways I will need to use all this information.
Ough I to mantain the professor's ID in object student
and then load the information when I need? Should I create a property just for storing the professorID, or I create a new
professor
object without loading all other data into it, just storing the ID
? Or neither? What's the best practice?
Thanks.
Upvotes: 0
Views: 60
Reputation: 4143
Suggestion 1
Does your application requires to save & load the values of the objects & properties ( also known as "to serialize" or "to persist" ) ?
Does your application requires to have several instances of the same class ?
If the answer to this questions is true, seems you need to store your data in a D.B., as @larsmans suggest.
Suggestion 2
Another thing. You didn't mention what programming language are you using. In programming languages like C++, Delphi (Object Pascal), D, an object inside another object can be handled in 2 ways: as part of the object, or as a pointer to the subobject.
I suggest use pointers to objects approach, for your scenario.
In programming languages like Java, PHP, C#, VB.NET, there is this concept called references, which in practical terms, its the same as pointer to objects, so you don't need to do anything else.
Non pointer example:
class SubClass
{
public:
int X;
int Y;
}; // class SubClass
class MainClass
{
public:
int Color;
SubClass SubObject;
public:
/* constructor */ MainClass()
{
this.Color = 7;
this.SubObject();
} // /* constructor */ MainClass(...)
/* destructor */ ~MainClass()
{
this.~SubObject();
this.Color = 0;
} // /* destructor */ MainClass(...)
}; // class MainClass
void main()
{
MainClass* MainObject = new MainClass();
MainObject->Color = 5;
MainObject->SubObject.X = 19;
MainObject->SubObject.Y = 32;
delete MainObject();
} // void main(...)
Pointer to objects example:
class SubClass
{
public:
int X;
int Y;
}; // class SubClass
class MainClass
{
public:
int Color;
SubClass* SubObject;
public:
/* constructor */ MainClass()
{
this.Color = 7;
this.SubObject = new SubClass();
} // /* constructor */ MainClass(...)
/* destructor */ ~MainClass()
{
delete this.SubObject();
this.Color = 0;
} // /* destructor */ MainClass(...)
}; // class MainClass
void main()
{
MainClass* MainObject = new MainClass();
MainObject->Color = 5;
MainObject->SubObject->X = 19;
MainObject->SubObject->Y = 32;
delete MainObject();
} // void main(...)
Cheers.
Upvotes: 2
Reputation: 32296
In the general case, you will need some entities joining the entities from your domain. Say, you will have a StudentRegistry
that will hold the correspondence between the students and their professors/lecturers. This indeed resembles an RDBMS schema design, so you can refer to the ER DB design method (obviously, you will have classes instead of tables.)
Upvotes: 1