STL(标准模板库)中使用的设计模式

发布于 2024-08-30 00:09:07 字数 1540 浏览 1 评论 0原文

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

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

发布评论

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

评论(7

戏剧牡丹亭 2024-09-06 00:09:07

我希望你的意思是“哪些设计模式可以在STL中识别”。

STL 堆栈是一个容器适配器。适配器是一种设计模式。迭代器也是一种设计模式。 STL函数对象与命令模式相关。

模式:

  1. Adapter(容器适配器)
    • 堆栈
    • 队列
    • 优先队列
  2. Iterator
  3. Command + Adapter(函数适配器)
  4. Iterator + Adapter(迭代器适配器)
    • 反向迭代器
    • 插入迭代器
    • 流迭代器
  5. 模板方法(使用用户指定函数的STL算法)
  6. 哪种创建模式? (分配器)

这些模式的实现方式与面向对象设计中的实现方式非常不同。 Josuttis 写道“STL 概念与面向对象编程的原始思想相矛盾”。这就是导致您的问题出现混乱的原因。

I hope you mean, "which design patterns can be identified in the STL".

The STL stack is a container adapter. An adapter is a design pattern. The iterator is also a design pattern. The STL function objects are related to the command pattern.

Patterns:

  1. Adapter (container adapters)
    • stack
    • queues
    • priority queues
  2. Iterator
  3. Command + Adapter (function adapters)
  4. Iterator + Adapter (iterator adapters)
    • reverse iterators
    • insert iterators
    • stream iterators
  5. Template Method (STL algorithms using user-specified functions)
  6. Which creational pattern? (Allocators)

The way these patterns are implemented is very different from the way they are implemented in an object oriented design. Josuttis wrote "the STL concept contradicts the original idea of object-oriented programming". This is what is causing the confusion around your question.

攒眉千度 2024-09-06 00:09:07

迭代器模式在STL中被大量使用。

The Iterator pattern is used pretty heavily in the STL.

请你别敷衍 2024-09-06 00:09:07

STL 广泛使用模板。 GoF 调用此参数化类型。模板对于自定义设计模式解决方案或提出新的直观解决方案非常有用。 (有关更多详细信息,请参阅“设计模式:元素”中的“继承与参数化类型”部分可重用面向对象软件”)。熟悉 STL(和 boost)的好处是它们是了解模板的良好来源(和元编程)在 C++ 中,反过来又可以用于设计更好的设计。

STL makes extensive use of templates. GoF call this parameterized types. Templates are useful for customizing a design pattern solution or in coming up with a new, intuitive solution. (For more details, see the section "Inheritance versus Parameterized Types" in "Design Patterns: Elements of Reusable Object-Oriented Software"). The advantage of getting familiar with STL (and boost) is that they are a good source to learn about templates (and meta-programming) in C++, which in turn can be used in devising better designs.

远山浅 2024-09-06 00:09:07

我认为你的问题是设计模式没有在STL中实现。它们可以用 C++ 实现并使用 STL 中的容器和算法,但 STL 和设计模式没有任何其他方式相关。

我的建议是通过阅读Nicolai Josuttis 的优秀著作《C++ 标准库》来了解 STL:教程和参考STL教程和参考指南。这将有助于了解 STL 可以为您做什么。然后利用您对 STL 的了解,深入研究在 C++ 中实现设计模式。

I think that your problem is that design patterns are not implemented in STL. They can be implemented in C++ and use containers and algorithms from STL but STL and Design Patterns are not related in any other way.

My advice would be to learn about STL by reading something like Nicolai Josuttis' excellent book The C++ Standard Library: A Tutorial and Reference or STL Tutorial and Reference Guide. This will help in learning what the STL can do for you. Then dig into implementing design patterns in C++ using your knowledge about the STL.

○愚か者の日 2024-09-06 00:09:07

C++11开始,我们有了三种智能指针,即shared_ptr、unique_ptr和weak_ptr,它们背后的模式是:代理模式

From C++11, we got threes kinds of smart pointer, i.e, shared_ptr,unique_ptr and weak_ptr, the pattern behind them is :Proxy pattern.

苯莒 2024-09-06 00:09:07

std::vector::referencestd::bitset::reference 也是代理模式的示例

std::vector<bool>::reference and std::bitset::reference are also examples of Proxy pattern

你曾走过我的故事 2024-09-06 00:09:07

在现代 STL 中,我从头到尾意识到:

  1. 迭代器被广泛传播。它们存在于每个容器中
  2. 单例(例如 std::cout)
  3. 工厂方法(std::make_unique、std::make_shared 等)
  4. 访问者(std::visit 和 std::variant)
  5. 适配器(std::stack 适应集合)您提供模板类型)
  6. 命令(std::function)
  7. 责任链(范围?)
  8. 装饰器(std::reference_wrapper?)

What I am aware of from the top of my head in modern STL is:

  1. Iterators are widely spread. They are present in every container
  2. Singleton (std::cout for example)
  3. Factory methods (std::make_unique, std::make_shared etc)
  4. Visitor (std::visit with std::variant)
  5. Adapters (std::stack adapts a collection you provide as template type)
  6. Command (std::function)
  7. Chain of responsibility (ranges?)
  8. Decorator (std::reference_wrapper?)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文