vendredi 30 septembre 2022

Should I create an object and call function, or create static class function that takes in object?

I currently have this class:

class typeX_handler {
    SomeObject& toBeAltered;
    void function();
}

So in my main I am doing something like this:

SomeObject result = SomeObject() 

// Initialize all my handlers
TypeX_handler x = TypeX_handler(result);
...

// Some condition where I use this handler 
if (... == "type_x") {
    x.function();
}

else if ... Repeat for many other handlers

So by doing this, I'm essentially updating my SomeObject which I can return at the end. My question is that is it better to create a static function for the typeX_handler class as such:

class typeX_handler {
    static void function(SomeObject& toBeAltered);
}

// In the if-statement I call this instead
typeX_handler::function(result);

So that I do not need to initialize all of my handlers at the start? Or is it clearer that I initialize my handlers, because there may be cases where not all the handlers are utilized.

I did consider doing this in the if method:

if (... == "type_x") {
    // if (x is not initialized) { initalize x; }
    x.function();
}

But I'm also not sure how to implement this. As far as I am aware of, I have to utilize pointers to make x initially a NULL_PTR. Please advise on which design pattern to use!!

Aucun commentaire:

Enregistrer un commentaire