c++ - Modify the field of a class in various ways. Use decorators? -
i have construction this:
struct calibrationinputs { float eta; float rawcl_es0; float rawcl_es1; float rawcl_es2; float rawcl_es3; };
this input of big function
float calibrate(calibrationinput inputs)
i want apply calibrate
function using biases inputs, need way modify field of calibrationinputs
. easy task, want in way. doing is:
class biasbase { public: biasbase(float amount, std::string type) : m_amount(amount), m_type(type) { }; virtual void bias(calibrationinputs & calibration_inputs) = 0; std::string get_name() const { homecoming m_type + "_" + get_amount_string(); }; float get_amount() const { homecoming m_amount; }; protected: float m_amount; std::string m_type; }; class biase2 : public biasfixed { biase2(float amount) : biasfixed(amount, "e2") { }; void bias(calibrationinputs & calibration_inputs) { calibration_inputs.rawcl_es2 *= (1 + m_amount); } }; class biase1 ... class verycomplexbias ... class randombiase2 ...
i'm using them as:
calibrationinput input; calibrationinput input_bias_e1 = input; ... biase1 bias_e1(0.01); bias_e1.apply_bias(input_bias_e1); float result = calibrate(input); float result_bias_e1 = calibrate(input_bias_e1); ...
the problem is: if want combine bias
classes, apply @ same time 2 or more biases? don't want overcomplicate easy problem, maybe can utilize decorators. problem every decorator class need inherite calibrationinputs
, i'm duplicating every time fiels, no?
what design problem?
i thought question while on friday, not able find realy solution works in various ways. suggest using binders (std::mem_fn, boost.bind boost.phoenix) transform members function objects. after step can create:
polymorphic function objects (std::function, boost.function) pass around these "members" in uniform way use these function objects in templates combine them , create more complex function objects.if need additional runtime configuration of bias types think implementing kind of interpreter using boost.spirit. hope helps.
c++ design-patterns
No comments:
Post a Comment