Small Project Organization
Dependency Object
A small object can hold a dependency value and expose the operation that uses it.
dependency
Instead of hiding a value in a global variable, store it in an object that receives it directly.
method
A const method can use the stored value without changing the object.
Dependency Object
dependency_object.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
struct Multiplier {
int factor;
int apply(int value) const {
return value * factor;
}
};
int main() {
int factor = 3;
Multiplier multiplier{factor};
int input = 7;
int output = multiplier.apply(input);
std::cout << "factor=" << factor << std::endl;
std::cout << "input=" << input << std::endl;
std::cout << "output=" << output << std::endl;
return 0;
}
#include <iostream>
struct Multiplier {
int factor;
int apply(int value) const {
return value * factor;
}
};
int main() {
int factor = 2;
Multiplier multiplier{factor};
int input = 7;
int output = multiplier.apply(input);
std::cout << "factor=" << factor << std::endl;
std::cout << "input=" << input << std::endl;
std::cout << "output=" << output << std::endl;
return 0;
}
#include <iostream>
struct Multiplier {
int factor;
int apply(int value) const {
return value * factor;
}
};
int main() {
int factor = 5;
Multiplier multiplier{factor};
int input = 7;
int output = multiplier.apply(input);
std::cout << "factor=" << factor << std::endl;
std::cout << "input=" << input << std::endl;
std::cout << "output=" << output << std::endl;
return 0;
}
factor ← 3, multiplier ← (empty), input ← 7
11int main() {12 int factor→ 3 = 3; //@factor=2, 51314 Multiplier multiplier→ (empty){factor3};15 int input→ 7 = 7;16 int output = multiplier(empty).apply(input7);int apply(int value) const
6int apply(int value7) const {7 return value7 * factor3;8}output ← 21
15 int input = 7;16 int output→ 21 = multiplier(empty).apply(input7);1718 std::cout << "factor=" << factor3 << std::endl;19 std::cout << "input=" << input7 << std::endl;20 std::cout << "output=" << output21 << std::endl;21 return 0;22}outputfactor=3 input=7 output=21
factor ← 2, multiplier ← (empty), input ← 7
11int main() {12 int factor→ 2 = 2;1314 Multiplier multiplier→ (empty){factor2};15 int input→ 7 = 7;16 int output = multiplier(empty).apply(input7);int apply(int value) const
6int apply(int value7) const {7 return value7 * factor2;8}output ← 14
15 int input = 7;16 int output→ 14 = multiplier(empty).apply(input7);1718 std::cout << "factor=" << factor2 << std::endl;19 std::cout << "input=" << input7 << std::endl;20 std::cout << "output=" << output14 << std::endl;21 return 0;22}outputfactor=2 input=7 output=14
factor ← 5, multiplier ← (empty), input ← 7
11int main() {12 int factor→ 5 = 5;1314 Multiplier multiplier→ (empty){factor5};15 int input→ 7 = 7;16 int output = multiplier(empty).apply(input7);int apply(int value) const
6int apply(int value7) const {7 return value7 * factor5;8}output ← 35
15 int input = 7;16 int output→ 35 = multiplier(empty).apply(input7);1718 std::cout << "factor=" << factor5 << std::endl;19 std::cout << "input=" << input7 << std::endl;20 std::cout << "output=" << output35 << std::endl;21 return 0;22}outputfactor=5 input=7 output=35