c++ - 我应该如何决定是否构建 "protected interface"?

标签 c++ inheritance interface access-specifier

发件人:http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.9

Three keys: ROI, ROI and ROI.

Every interface you build has a cost and a benefit. Every reusable component you build has a cost and a benefit. Every test case, every cleanly structured thing-a-ma-bob, every investment of any sort. You should never invest any time or any money in any thing if there is not a positive return on that investment. If it costs your company more than it saves, don't do it!

Not everyone agrees with me on this; they have a right to be wrong. For example, people who live sufficiently far from the real world act like every investment is good. After all, they reason, if you wait long enough, it might someday save somebody some time. Maybe. We hope.

That whole line of reasoning is unprofessional and irresponsible. You don't have infinite time, so invest it wisely. Sure, if you live in an ivory tower, you don't have to worry about those pesky things called "schedules" or "customers." But in the real world, you work within a schedule, and you must therefore invest your time only where you'll get good pay-back.

Back to the original question: when should you invest time in building a protected interface? Answer: when you get a good return on that investment. If it's going to cost you an hour, make sure it saves somebody more than an hour, and make sure the savings isn't "someday over the rainbow." If you can save an hour within the current project, it's a no-brainer: go for it. If it's going to save some other project an hour someday maybe we hope, then don't do it. And if it's in between, your answer will depend on exactly how your company trades off the future against the present.

The point is simple: do not do something that could damage your schedule. (Or if you do, make sure you never work with me; I'll have your head on a platter.) Investing is good if there's a pay-back for that investment. Don't be naive and childish; grow up and realize that some investments are bad because they, in balance, cost more than they return.

好吧,我不明白如何将其与 C++ 保护接口(interface)相关联。

请提供任何真实的 C++ 示例以说明此常见问题解答的内容。

最佳答案

首先,永远不要将任何编程引用视为权威。曾经。一切都是别人的意见,最后你应该做最适合你的事情。

所以,就是说,这篇文章基本上想说的是“不要使用花费你的时间多于节省的时间的技术”。他们描述的“ protected 接口(interface)”的一个示例如下:

class C {
    public:
        int x;
};

现在,在 Java 中,所有 Java EE 编程书籍都会告诉您始终像这样实现该类:

class C {
    public:
        int getX() { return x; }
        void setX(int x) { this.x = x; }
    private:
        int x;
};

...这是适当的封装(技术术语:简化一点,它意味着最小化离散部分之间的共享)的实现。使用您的代码的类担心您有一些方法获取设置 一个整数,而不是它实际上作为 int 存储在类(class)。因此,如果您使用访问器方法,您以后可以更好地更改底层实现:也许您希望它从网络中读取该变量?

但是,这会产生大量额外代码(就字符而言),并且实现起来会有些额外的复杂性。 正确地做事实际上是有代价的!就代码的正确性而言,这不是成本 - 直接 - 但你花了几分钟时间做得比你本可以花在做其他事情上的时间“更好”,而且维护你编写的所有内容所涉及的工作量不为零,无论多么微不足道。

因此,在我看来,这段话中所说的是一个很好的建议:当你去做某事时,请始终仔细检查,确保你从中得到的比你投入的更多。理智检查您没有遵循一种会损害您作为程序员或人类的实际效率的理想。

这些建议将在任何编程语言和任何行业为您提供良好的服务。

关于c++ - 我应该如何决定是否构建 "protected interface"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9138713/

相关文章:

c++ - 关键部分在线程或主程序中更好?

java - 协调 thrift 中继承的缺失与 Java/Scala 中的方法定义

c# - 通用接口(interface)约束

C# 日志设计 : Extension method, 的替代方案?

spring - 实现 BeanPostProcessor spring 5 + java 9

c++ - 如何在类中创建模板函数? (C++)

c++ - 在文本文件中查找 10 个最大数量行,其中包含 shipment ID 、 UPC code 、 quantity

javascript - javascript中继承类的最佳实践

c++ - 非多态继承对性能/内存使用有影响吗?

c++ - 为什么这个模板类不能正常工作?