此 c++ 的名称实施模式

发布于 2024-11-30 00:36:08 字数 359 浏览 0 评论 0原文

C++ 中有一定的实现模式,我在下面描述,它用于 std::iostream 库和其他类似的库。

有人能记得这个图案的名字吗?

该模式的描述如下:
- 有中央类 IO 用于数据输出或数据转换(例如 std::ostream)。
- 对于定义输出转换的每个应用程序类,“转换器”是全局函数,而不是 IO 的成员函数。这种模式的动机是

(1)IO 的设计者希望它“完成”,当添加另一个带有转换器的应用程序类时不需要任何更改,

(2)因为您希望 IO 成为一个小的可管理类,而不是一个班级有 100 名成员和 1000 条线路。当 IO 类和多个“用户”类之间需要解耦时,这种模式很常见。

这个图案的名字是什么?

There is certain implementation pattern in C++, that I describe below, it is used in std::iostream library and other similar libraries.

Can anybody recall name of this pattern?

The pattern is described like this:
- There is central class IO used for output of data, or for conversion of data (e.g, std::ostream).
- for every application class for which output conversion is defined, "converters" are GLOBAL functions, not member functions of IO. The motivation for this pattern is

(1) designer of IO wants to have it "finished", not needing any changes when another application class with convertor is added, and

(2) because you want IO to be a small manageable class, not a class with 100 members and 1000s of lines. This pattern is common when decoupling is needed between IO class and multitude of "user" classes.

What is name of this pattern?

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

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

发布评论

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

评论(2

我为君王 2024-12-07 00:36:08

看起来这是 Herb Sutters 的“接口原则”,

至少我从他的一本书中读到它

接口必须是最小的,所有不需要私有数据(用于编译或运行速度)的函数都应该在外部函数中。

looks like it's the Herb Sutters' "Interface Principle"

at least I read it from one of his books

the interface must be minimal, all functions, which do not need private data (for compiling or runtime speed), should be in outer functions.

背叛残局 2024-12-07 00:36:08

这根本不是一种设计模式。

设计模式不依赖于编程语言。您所描述的内容已完成,因为类 std::ostream 来自库。所以你不能方便地添加“operator <<(MyClass &ob)”成员函数。

代替设计模式的正确术语是“惯用语”。请参阅例如: http://en.wikibooks.org/wiki/C++_Programming/习语http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms(不确定你的案例是否被列出,乍一看我没有找到它)

This is not a design pattern at all.

Design patterns are not tied to a programmng language. What you describe is done because the class std::ostream comes from a library. So you can not conveniently add "operator <<(MyClass &ob)" member functions.

The proper term instead of design pattern is "idiom". See e.g.: http://en.wikibooks.org/wiki/C++_Programming/Idioms or http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms (not sure if your case is listed, on a first glance I did not find it)

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