阅读罗伯特·马丁(Robert Martin)的“干净的体系结构”之后,我想为计算繁重的实时应用程序实现依赖项反转。更精确地:
将接口/ actract类与虚拟函数一起使用似乎会为实时场景增加过多的开销(表查找等)。
您推荐哪些实施方式?
关于Robert's example,我想出了一种基于模板的简单方法:
#include "stdio.h"
class TestReader {
public:
int read() {
int input = 5;
printf("Input:\t%d\n", input);
return input;
}
};
class ConsoleReader {
public:
int read() {
int input;
printf("Enter Integer:\t");
scanf("%d", &input);
printf("Input:\t%d\n", input);
return input;
}
};
class ConsoleWriter {
public:
void write(int input) {
printf("Output:\t%d\n", input);
}
};
template <typename TRead, typename TWrite>
class Encrypt {
public:
void transform() {
int input = IRead.read();
printf("Encrypt...\n");
int output = input + input;
IWrite.write(output);
};
private:
TRead IRead;
TWrite IWrite;
};
int main() {
Encrypt<ConsoleReader, ConsoleWriter> b;
b.transform();
}