Avoiding if statements about static logical for logical decisions

I have a class whose member itemType is set only once and never changes, but it is used in many if statements to decide which function to call. Since itemType is set only once, there is a way to avoid if statements, where in the class. This will simplify and clean the code, and overhead during verification will also be saved as a bonus. I was thinking of a taht pointer function, which I can initialize in the constructor based on the value of itemType. Is there an alternative and better way to do this?

Please note that the source class and code base are large, and I cannot get around creating child classes based on the itemtype type.

enum ItemTypes
{
    ItemTypeA,
    ItemTypeB,
};


class ItemProcessing
{
public:

    //This function is called hundreds of times
    void ProcessOrder(Order* order)
    {
        //This member itemType is set only once in the constructor and never modified again
        //Is there a way to not check it all the time??
        if (itemtype == ItemTypes::ItemTypeA )
        {
            ProcessTypeA(order)
        }
        else if (itemtype == ItemTypes::ItemTypeB )
        {
            ProcessTypeB(order)
        }
    }

    ItemProcessing(ItemTypes itype)
    {
        itemtype = itype; //can  I do something here like setting a function pointer so I dont have to check this property in ProcessOrder() and call the relevant function directly.
    }

private:

    ItemTypes itemtype;
    void ProcessTypeA(Order*);
    void ProcessTypeB(Order*);
};
+4
3

, itemtype, :

typedef void(*ProcessType_func_t)(Order *);

ProcessType_func_t processType_f[] = {
    ProcessTypeA,
    ProcessTypeB
};

:

void ProcessOrder(Order *order) {
    ProcessType_f[itemtype](order);
}

, , .

struct {
    ProcessType_func_t processType_f,
    OtherType_func_t otherType_f,
    ...
} dispatchTable[] = {
    { ProcessTypeA, OtherTypeA, ... },
    { ProcessTypeB, OtherTypeB, ... }
};

:

dispatchTable[itemtype].processType_f(order);

, - , :

class Processor { // abstract base class
    public:
        virtual void Process(Order *order) = 0;
};

class ProcessorA {
    public: 
        void Process(Order *order) {
            ProcessTypeA(order);
        }
}


class ProcessorB {
    public: 
        void Process(Order *order) {
            ProcessTypeB(order);
        }
}

-

Processor *processor;

, itemtype

ItemProcessing(ItemTypes itype)
{
    itemtype = itype;
    if (itemtype == ItemTypeA) {
        processor = new ProcessorA;
    } else {
        processor = new ProcessorB;
    }
}

:

processor->Process(order);

, , itemtype - .

, , ++ OO.

+4

-, .
, , , ( , ).

. . , .
, .

+1

In C ++, a function pointer must imitate a virtual function and inheritance. (Polymorphism) Define a virtual class, including pure virtual methods processOrder ( Order* ordre); AND define a subclass for each value of your enumeration. You can use the abstract factory template to create this object or, if necessary.

I can write the code if I wish.

+1
source

Source: https://habr.com/ru/post/1621643/


All Articles