In object oriented programming we have uses, has-a, is-a relationships. I wonder what would be the relation between earth and the sun, Earth revolves around the sun?
What is the best way of learning object oriented analysis, design and programming?
Objects, classes and is-a relationships
In your object oriented domain model there are two main objects:
Earth
of class Planet
(instantiation, is-a)Sun
of class Star
(instantiation, is-a) Planet
and Star
are special kinds of CelestialObject
(specialization, is-a) Association of classes and objects
There is an association between Sun
and Earth
: Earth
rotates around the Sun
, captured by its gravity.
If we think in more general terms, any Star
could have several Planet
that rotate around. It may as well have no Planet
at all. Conversely, most of the Planet
rotate around one single Star
. Let's keep it simple for now, despite recent discoveries, which have confirmed that in very rare cases a Planet
could be associated with several Star
.
Such association has two point of view: from the Planet
we can say has-a-Star
and from the Star side, we could say has-a-Planet
.
How to represent associations in a world of objects ?
A simple way to represent a has-a association is to keep a reference to the associated object. This works only for singular association, e.g. the object Earth
would keep a reference to Sun
.
Another way to represent a has-a association is to keep a list (more generally, a container) of reference to the associated objects. Sun
would then have a list of associated Planet
composed of Venus
, Earth
, Mars
, etc...
Sometimes an association could be more complex and bear some data that is unique to the link it represents. For example, Earth
is associated with the Sun
and it turns around it in 365,25 days. To keep things simple, you could just consider that it's an attribute of the Planet
in the solar system. But if we'd take this seriously, this duration is in reality not a property of the Earth
, because the Earth
also turns around the center of the galaxy but with a different duration. It's not a property of the Sun
either, because Mars
turns also around the Sun
, but in 686,96 days.
In such cases, you can consider that the association is itself a class. An instantiation if it would be an object with a orbit duration of 365,25 days, and has-a first extremity Sun
and a second extremity Earth
More about these core concepts
To learn more about these core concepts, and in a language independent fashion, you could have a look at the UML class diagram. It's purpose is to represent exactly these kind concepts. You may also have a look at this blog, which gives a nice object of classes for celestial objects, although it doesn't address the associations.
How to use the model in a simulation ?
You need to choose an OO language to implement your model. Keep a container of celestial objects. Each of this object should have some coordinates, a rotation speed, and a method that updates the coordinates based on its rotation speed and the elapsed time. Your simulation would then consist of a loop (representing the elapsed time), and at each iteration, you'd go through your list of celestial objects and for each one update the coordinates, and display the result.