未封装意味着不可更改?

发布于 2024-11-18 01:33:13 字数 236 浏览 5 评论 0原文

我在Effective C++中看到了这一行:

公开意味着未封装,实际上,未封装意味着不可更改,尤其是 对于广泛使用的类。然而广泛使用的类最需要封装,因为 他们是能够从用更好的实现替代一种实现的能力中获益最多的人。 一个

作者所说的“公开意味着未封装,实际上,未封装意味着不可更改”是什么意思?

未封装的如何是不可更改的?

I came across this line in Effective C++:

Public means unencapsulated, and practically speaking, unencapsulated means unchangeable, especially
for classes that are widely used.Yet widely used classes are most in need of encapsulation, because
they are the ones that can most benefit from the ability to replace one implementation with a better
one

What does the author mean by "Public means unencapsulated, and practically speaking, unencapsulated means unchangeable"?

And how is unencapsulated unchangeable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凡尘雨 2024-11-25 01:33:13

总体思路很简单。如果你公开某些东西,那么有人可以而且很可能会使用它。因此,如果您更改一些公开的内容,则所有使用它的代码都会立即中断。破坏别人的代码是不好的;这往往会导致他们不想再使用你的代码,因为现在你迫使他们重写所有的东西,只是因为你想使用不同的类型或其他东西。

公共接口是类的实现和类的用户之间的契约。更改合同,尤其是在没有提前通知的情况下,被认为是非常粗鲁的。

如果您的所有代码都是内部的,那就没问题。但如果不是,如果您正在制作一个库供其他人使用(无论是在本地还是只是出售库),那么人们不太可能对界面更改感到高兴。

这不是 C++ 规则的问题;而是 C++ 规则的问题。这只是界面设计规则的问题。由于公共内容是界面的一部分,因此您必须小心公开的内容。

The general idea is simple. If you make something public, then someone can and probably will use it. Therefore, if you change something that is public, all of the code that uses it immediately breaks. Breaking people's code is bad; it tends to lead to them not wanting to use your code anymore, since now you've forced them to rewrite all of their stuff just because you wanted to use a different type or something.

The public interface is a contract between the implementation of the class and the user of it. Changing that contract, particularly without advanced notice, is considered very rude.

If all of your code is internal, that's fine. But if it's not, if you're making a library for others to use (whether locally or just selling a library), then people are less likely to be happy about interface changes.

It isn't a matter of rules of C++; it's simply a matter of rules of interface design. Since public things are part of the interface, you must be careful about what you make public.

雪花飘飘的天空 2024-11-25 01:33:13

封装是指只能通过接口方法访问类的数据成员的思想。使用方法的效果是“隐藏”数据成员的实际实现,以便您可以更改它,而无需更改通过接口方法使用该类的代码(前提是您不更改它们的方式)定义)。

另一方面,如果您不使用接口方法来隐藏数据成员实现,则所有使用它的代码都需要在数据成员发生任何更改之前进行修改。

假设您有一个类,其中包含包含名称列表的字符串向量。如果您公开该向量,那么所有其他类都可以决定直接使用它,并通过向量中的索引访问它包含的字符串(例如,索引充当识别字符串的键)。

稍后,您可能决定需要一个映射来管理所有这些字符串(您的要求已更改)。您将数据成员定义更改为映射,您的代码将不再编译。这就是“几乎不可改变”的含义。

通过封装来管理此问题的“正确”方法是使数据成员私有并定义一个接口方法,例如:

std::string getStringWithKey(int index);

此方法将在第一个实现中访问向量,在第二种情况下访问映射。所有这一切都以透明的方式进行:使用该方法的代码(而不是直接访问数据成员)不需要修改,因此您可以根据需要更改数据成员实现,几乎不需要任何成本。

这是过于简单化了,因为界面设计不是一件简单的事情,界面也会发生变化,但我希望它有助于澄清事情。

Encapsulation is the idea that you can access the data member of a class only through interface methods. The effect of using a method is "hiding" the actual implementation of the data member, so that you can change it without the need to change the code that uses that class through the interface methods (provided you don't change the way they are defined).

On the other hand, if you don't use interface methods to hide the data member implementation, all your code that uses it will need to be modified in front of any change in the data member.

Say that you have a class with a vector of strings containing a list of names. If you make public that vector, then all other classes can decide to directly use it, and access the strings it contains by their index in the vector (the index acts as a key to identify the string, say).

Later, you could decide you need a map to manage all those strings (your requirement have changed). You change the data member definition to a map and your code will not compile anymore. This is the meaning of "practically unchangeable".

The "right" way to manage this through encapsulation is making the data member private and define an interface method like:

std::string getStringWithKey(int index);

this method will access the vector in the first implementation, and the map in the second case. All this in a transparent way: the code that uses the method (instead of accessing directly the data member) will not need to be modified, so you can change the data member implementation as you like at almost no cost.

This is an oversimplification, because design of interfaces is not a simple matter and interfaces also do change, but I hope it helps clarifying things.

盛夏尉蓝 2024-11-25 01:33:13

我想作者应该会给出最好的答案。我的猜测是,他的意思是,如果您声明一个公共成员并在代码中的许多其他地方使用它,那么如果您后来决定更改该成员,则更改所有这些地方将是一项艰巨的工作。

I guess the best answer would be given by the author. My guess is that he means that if you declare a public member and use it on many other places in your code, then it would be hard work to change all those places if you later decide to change that member.

赠佳期 2024-11-25 01:33:13

他指的是数据成员,他说将数据成员作为公共接口的一部分公开公开意味着您永远无法更改它们的性质,即名称和类型。

He is referring to data members, and he is saying that publicly exposing data members as part of the public interface means that you can never change their nature, i.e. name and type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文