lundi 8 août 2016

Programming pattern for components that are toggleable at runtime

I'm wondering if there is some kind of logical programming pattern or structure that I should be using if sometimes during runtime a component should be used and other times not. The obvious simple solution is to just use if-else statements everywhere. I'm trying to avoid littering my code with if-else statements since once the component is toggled on, it will more than likely be on for a while and I wonder if its worth it to recheck if the same component is active all over the place when the answer will most likely not have changed between checks.

Thanks

A brief example of what I'm trying to avoid

class MainClass
{
public:
    // constructors, destructors, etc

private:
    ComponentClass m_TogglableComponent;
}

// somewhere else in the codebase
if (m_TogglableComponent.IsActive())
{
    // do stuff
}

// somewhere totally different in the codebase
if (m_TogglableComponent.IsActive())
{
    // do some different stuff
}   

Aucun commentaire:

Enregistrer un commentaire