I want to illustrated my question by way of a (hopefully) representative example
ID: 641952 • Letter: I
Question
I want to illustrated my question by way of a (hopefully) representative example.
Lets say I have a situation where I am developing a class library in C# to be used in some simulations. I want to define the planets (Earth, Mars, Saturn, etc.) and have each of them have some properties and methods that are common in interface (name, signature, etc.) but unique in implementation per-each. I.e. Earth.Mass is not equal to Mars.Mass.
So far no big deal.
I want to have some other classes/methods/algorithm that accept as an argument a planet and use its defined properties and method:
double MyApparentWeight(Planet someplanet, double MassInKG)
double DistanceFromInKM(Planet someplanet, Planet originplanet)
Planets could be used all over the place by someone using this library. All "Earth"s should be identical and all "Saturn"s should be identical. Obviously the "Earth"s are distinct from the "Saturn"s.
I also want a user of the library to be able to easily create a new planet (e.g. Vulcan, Endor, Tralfamadore, Persephone, Rupert, etc.). It should be forced to supply the properties and methods of the existing ones and hence be useable by the MyApparentWeight and DistanceFromInKM methods.
So the base level approach is that there is a Planet class and then the specific ones (Earth, Vulcan, etc.) simply inherit from that. In this case though a user needs to either create an instance of Earth and carry it around or create a new one every time it is needed. That could be unnecessarily clunky (especially in light of the expected users) and possibly a performance hit since some Planets could have initialization steps of reading date from files, etc.
The next idea was the above but with a singleton paradigm. That's basically what I've done so far. This seems to be dictating internal implementation to any new Planets that are created (and again some what of a concern given the potential level of some users).
This project is how I've been cutting my C#/.NET teeth. My initial naive approach was to try to do static classes with interfaces or inheritance. Obviously I learned quickly the folly of that path. At a high level that is the kind of paradigm I would have wanted though.
Is there some other design pattern that I'm missing that could cover this type of situation?
Appreciate any suggestions.
Explanation / Answer
I would have a generic planet class, with read-only properties that were set by constructor parameters.
class Planet
{
public string Name { get; private set; }
public double Mass { get; private set; }
public Planet(string name, double mass)
{
this.Name = name;
this.Mass = mass;
}
}
I would then have a PlanetFactory class for generating well known planets:
static class PlanetFactory
{
public static Planet CreateEarth()
{
return new Planet("Earth", 597219000000000000000000);
}
public static CreateByName(string name)
{
if (name.Equals("Earth")) return CreateEarth();
}
}
I would then have a manager class to hold all of your planets:
class PlanetManager
{
private Dictionary<string, Planet> planets;
public GetPlanet(string name)
{
if (!planets.ContainsKey(name))
{
planets.add(name, PlanetFactory.CreateByName(name));
}
return planets[name];
}
}
This approach means that you can easily add new well-known planets and provide a mechanism for adding new planets through the PlanetManager class (though you would need a different method to do so).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.