Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate a subclass, instead, when calling the creation of a class, depending on some logic
    text
    copied!<p><em>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.</em></p> <p>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.</p> <p>More specifically, I am making a <a href="https://en.wikipedia.org/wiki/Magic_square" rel="nofollow">Magic Square</a> solver as a learning exercise and:</p> <ul> <li>I want to have a <code>MagicSquare</code> class that will contain the logic of a MagicSquare.</li> <li>I want to have <code>OddMagicSquare</code> and <code>EvenMagicSquare</code> subclasses of that class that will contain the logics of solving these two types of Magic Squares.</li> <li>I want to be able to call the creation of a <code>MagicSquare</code>, providing it's size, <code>n</code>, and have the intelligence within <code>MagicSquare</code> determine which subclass to create an instance of, instead of the generic, top, class <code>MagicSquare</code>.</li> </ul> <p>I understand that the intelligence to determine which subclass (<code>OddMagicSquare</code>/<code>EvenMagicSquare</code>) to create an instance of <em>can be</em> (and perhaps would be easier to implement if it would be) <strong>outside</strong> of <code>MagicSquare</code>. The reason that I want it to be inside <code>MagicSquare</code> 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 <code>MagicSquare</code> class.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload