The question that I posted initially was lacking so here is an explanation that I hope will satisfy everyone and cancel some down votes that I received on it.
I would like to have an intelligence inside a class, that with the creation of an instance, it is decided that the instance would be of a different class, which is one of the subclasses of this class, according to some logic.
More specifically, I am making a Magic Square solver as a learning exercise and:
- I want to have a
MagicSquare
class that will contain the logic of a MagicSquare. - I want to have
OddMagicSquare
andEvenMagicSquare
subclasses of that class that will contain the logics of solving these two types of Magic Squares. - I want to be able to call the creation of a
MagicSquare
, providing it's size,n
, and have the intelligence withinMagicSquare
determine which subclass to create an instance of, instead of the generic, top, classMagicSquare
.
I understand that the intelligence to determine which subclass (OddMagicSquare
/EvenMagicSquare
) to create an instance of can be (and perhaps would be easier to implement if it would be) outside of MagicSquare
. The reason that I want it to be inside MagicSquare
is perhaps a gut feeling. I have a hunch that it would be more clean and tidy this way because the logic of determining which kind of Magic Square a certain Magic Square is, seems to me, to belong in the MagicSquare
class.