I'm quite green on object oriented programming and trying to get my style right.
Very often I have an object of which one the properties is a dictionary containing other objects. Call it class 'Team' that contains a class 'Player'.
Now let's say every time a Player is added to a Team I would like to have the average age of the team updated.
My favorite solution:
In Sub Main
I should say just
Team.add(Player)
Then in Team
the method add
is:
Public Sub Add(Player As CPlayer):
pPlayers.Add Player.Name, Player
Me.UpDateAvgAge(Player.Age)
End Sub
Now I can imagine at least one alternative way to do that which is:
In Main
:
Team.add(Player)
Team.UpDateAvgAge(Player.Age)
And the add
method should off course not have the Me.UpDateAvgAge(Player.Age)
line.
No need to say this is just simplest example. In real life there are a number of properties being 'updated' every time I 'add' something.
Is there a consensus among programmers on how to do this add/update? Any guidelines?
tks in advance!