Small Project Organization
Dependency Object
A small object can hold a dependency value and expose the operation that uses it.
Dependency Object
dependency_object.cpp
#include <iostream>
struct Multiplier {
int factor;
int apply(int value) const {
return value * factor;
}
};
int main() {
int factor = ;
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 = ;
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 = ;
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;
}
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.