我正在处理一个涉及 3D 绘图的相当大的项目,我想添加一些可视化工具(例如,查看对象的边界框)以使调试更容易。但是,我在决定如何解决这个问题时遇到了问题。
一个选项是创建一个公共(public)函数来绘制可视化工具,并在我从 UI 启用调试时调用此函数。这样做的好处是不修改现有函数,而是用新函数扩展类。缺点是“创建依赖关系”,正如我的一位同事所说,我们需要修改基类和所有派生类来添加此功能。
另一种选择是修改现有的绘图函数,以便它 处理可视化工具的绘图。这隐藏了实现细节,但在我看来,它也降低了代码的模块化程度。
还有一个选项是扩展类,在绘图函数中添加可视化工具,并在启用调试时交换类。 Mixins 会有帮助,但 C++ 不支持它。
执行此操作的最佳方法是什么?我正在寻找一种模块化且遵守 SOLID 原则的解决方案。
最佳答案
看起来您正在寻找“委托(delegate)模式”。参见 http://en.wikipedia.org/wiki/Delegation_pattern
In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object. There is an Inversion of Responsibility in which a helper object, known as a delegate, is given the responsibility to execute a task for the delegator.
另见 http://best-practice-software-engineering.ifs.tuwien.ac.at/patterns/delegation.html
关于c++ - 扩展类以调试 : public API, 隐藏实现或其他?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26625801/