Note that there are some explanatory texts on larger screens.

plurals
  1. POParametized class creation with assert of parameter presence. Use factory?
    primarykey
    data
    text
    <p>I spend about 2 hours reading a lot of factory related topics and I still am not sure if this would be the right approach.</p> <p>Here is the thing: I have an <strong>Enemy</strong> class that has contains member variables like <em>name</em>_ and <em>health</em>_. I want to create instances of it with different values of these member variables and my first approach was to get the parameters from a static array with the properties:</p> <pre><code>Enemy::Enemy(int type) : name_(properties[type].name), health_(properties[type].health) { ... } </code></pre> <p>The problem here is that I can't check if the array <em>properties</em> is already filled. I would have to check it from outside by calling a static <em>Enemy::initArray()</em> function but this would defeat the encapsulation of the <strong>Enemy</strong> class.</p> <p>Is this the time to create a factory that would initialize the <em>properties</em> array in it's constructor? And then create Enemies with:</p> <pre><code>Enemy* EnemyFactory::create(type); </code></pre> <p>I read that usually factories are created when you have complex class hierarchies or the factory caller only needs to know the interface of the created classes. I only need the encapsulation of the array creation and verification. Is there a "lighter" solution?</p> <p><strong>EDIT:</strong> I'll try to be more clear:</p> <p>1.) I think I know <strong>how</strong> to create a factory. The main question is whether there is an <strong>alternative</strong>!</p> <p>2.) I don't want to set the properties inside the object but use initialization lists. If I do the former I can just check the array in the constuctor and don't need the factory at all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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