I am implementing a loadbalancer that should choose balancing strategy based on some logic:
class LoadBalancer{
public:
bool get_backend_cpu_info(){// some complicate logic involve member backends b }
static void create(){
if successfully get_backend_cpu_info():
use LoadBalancerCPU.choose_backend()
else
use LoadBalancerRoundRobin.choose_backend()
}
choose_backend(){// depends on which type of loadbalancer we are using}
private:
vector<backends> b;
};
In c++ ,A straightforward forward approache is to make choose_backend a virtual function to get runtime polymorphism. However, the method get_backend_cpu_info() is also complicated process and need access to class member backends, which means I have to create the class itself. A another approch is to use factory method but still the get_backend_cpu_info() method need access to members before object creation. What is a good design pattern or class relationship in my case ???? This question is language independent and I ‘m just using C++ as a example.
I expect a good design pattern or class relationship design, prefereably with C++ code