129. Composition

Question.6

A developer constructs a Motor with an internally created PWM:

Option A -- Internal creation:

class Motor {
   PWM pwm;  // Created internally
public:
   Motor() : pwm(3) {}
};

Option B -- Dependency injection:

class Motor {
   PWM& pwm;  // Injected from outside
public:
   Motor(PWM& p) : pwm(p) {}
};

Why is Option B preferred for testable firmware?

Need Help? Refer to the Quick Guide below

Select Answer