C++程序员应该知道的著名设计模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3287052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Famous design patterns that a C++ programmer should know
提问by Prasoon Saurav
Possible Duplicate:
What C++ idioms should C++ programmers use?
可能的重复:
C++ 程序员应该使用哪些 C++ 习语?
After reading books like C++ Primer, Effective C++ and TC++PL I want to learn some important design patterns.
在阅读了 C++ Primer、Effective C++ 和 TC++PL 等书籍后,我想学习一些重要的设计模式。
So, what are the famous design patterns that every C++ programmer should know?
那么,每个 C++ 程序员都应该知道的著名设计模式是什么?
采纳答案by Steve314
The obvious answer is the Gang-Of-Four patterns from the famous book. These are the same patterns that get listed all over the place.
显而易见的答案是著名书中的四人组模式。这些是到处都列出的相同模式。
http://en.wikipedia.org/wiki/Design_Patterns
http://en.wikipedia.org/wiki/Design_Patterns
Beyond that, have a look around Martin Fowlers web site...
除此之外,请浏览 Martin Fowlers 网站...
There's a fair bit on there - the "famous" one is probably "dependency injection". Most others are pretty domain specific, though.
那里有相当多的东西 - “著名”的可能是“依赖注入”。不过,大多数其他人都非常特定于领域。
"Mixin layers" can be interesting for C++. A template class takes its own base as a template parameter, so that the template can be used to add the same functionality to many different classes, or as a composition method so that various features can be easily included/excluded for a library. The curiously recurring template trick is sometimes used as well (the original base is the final fully-composed class) so that the various mixin layers can do some degree of "reflection", so that intermediate methods can be defined in terms of fully-composed member types etc. Of course it can be a bit prone to unresolvable cyclic dependencies, if you're not careful.
“混合层”对于 C++ 来说可能很有趣。模板类将自己的基作为模板参数,以便模板可用于向许多不同的类添加相同的功能,或作为组合方法,以便可以轻松地为库包含/排除各种功能。有时也会使用奇怪的重复模板技巧(原始基础是最终的完全组合类),以便各种混合层可以进行某种程度的“反射”,从而可以根据完全组合的方式定义中间方法成员类型等。当然,如果您不小心,它可能有点容易出现无法解决的循环依赖关系。
回答by Michael
C++-specific ones: RAII and PIMPL.
C++ 特定的:RAII 和 PIMPL。
回答by Drew Hall
In no particular order, the Gang of Four patterns I see and use most, are probably the following:
没有特别的顺序,我看到和使用最多的四人组模式可能如下:
- Composite
- Template Method
- Abstract Factory
- Singleton (much hated, but everywhere)
- Visitor
- Builder
- Proxy
- 合成的
- 模板方法
- 抽象工厂
- Singleton(非常讨厌,但无处不在)
- 游客
- 建造者
- 代理
回答by CrociDB
回答by Nate W.
I suggest reading Head First Design Patterns. It's a fun read, and you'll learn about a lot of the common design patterns.
我建议阅读Head First Design Patterns。读起来很有趣,你会学到很多常见的设计模式。
回答by wilhelmtell
The think pattern. It's a silver bullet.
思维模式。这是一颗银弹。