简体   繁体   English

成语与模式

[英]Idiom vs. pattern

In the context of programming, how do idioms differ from patterns ? 在编程的背景下, 习语如何与模式不同?

I use the terms interchangeably and normally follow the most popular way I've heard something called, or the way it was called most recently in the current conversation, eg "the copy-swap idiom" and "singleton pattern". 我可以互换地使用这些术语,通常遵循我听过的最流行的方式,或者最近在当前对话中调用它的方式,例如“复制交换习语”和“单例模式”。

The best difference I can come up with is code which is meant to be copied almost literally is more often called pattern while code meant to be taken less literally is more often called idiom , but such isn't even always true. 我能想出的最好的区别是,几乎字面意思被复制的代码通常被称为模式,而代码意味着不那么字面意思的代码通常称为习语 ,但这种情况甚至不总是如此。 This doesn't seem to be more than a stylistic or buzzword difference. 这似乎不仅仅是一种风格或流行语的差异。 Does that match your perception of how the terms are used? 这符合您对术语使用方式的看法吗? Is there a semantic difference? 有语义差异吗?

Idioms are language-specific. 习语是特定于语言的。

Patterns are language-independent design principles, usually written in a "pattern language" (a uniform template) describing things such as the motivating circumstances, pros & cons, related patterns, etc. 模式是与语言无关的设计原则,通常用“模式语言”(统一模板)编写,描述诸如激励情境,利弊,相关模式等内容。

When people observing program development from On High (Analysts, consultants, academics, methodology gurus, etc) see developers doing the same thing over and over again in various situations and environments, then the intelligence gained from that observation can be distilled into a Pattern. 当人们从On High(分析师,顾问,学者,方法大师等)观察程序开发时,看到开发人员在各种情况和环境中一遍又一遍地做同样的事情,那么从该观察中获得的智能可以被提炼成模式。 A pattern is a way of "doing things" with the software tools at hand that represent a common abstraction. 模式是一种使用手头的软件工具“做事”的方式,它代表了一种共同的抽象。

Some examples: 一些例子:

  • OO programming took global variables away from developers. 面向对象编程使全局变量远离开发人员。 For those cases where they really still need global variables but need a way to make their use look clean and object oriented, there's the Singleton Pattern . 对于他们真正需要全局变量但需要一种方法使其使用看起来干净且面向对象的情况,有Singleton模式

  • Sometimes you need to create a new object having one of a variety of possible different types, depending on some circumstances. 有时您需要根据某些情况创建具有各种可能不同类型之一的新对象。 An ugly way might involve an ever-expanding case statement. 丑陋的方式可能涉及不断扩大的case陈述。 The accepted "elegant" way to achieve this in an OO-clean way is via the "Factory" or "Factory Method" pattern. 以OO清洁方式实现这一目标的“优雅”方式是通过“工厂”或“工厂方法”模式。

Sometimes, a lot of developers do things in a certain way but it's a bad way that should be disrecommended. 有时,许多开发人员以某种方式做事情,但这是一个不好的方式应该被推荐。 This can be formalized in an antipattern . 这可以在反模式中形式化。

Patterns are a high-level way of doing things, and most are language independent. 模式是一种高级的处理方式,大多数是与语言无关的。 Whether you create your objects with new Object or Object.new is immaterial to the pattern. 无论是使用new Object还是Object.new创建对象,对于模式都是无关紧要的。

Since patterns are something a bit theoretical and formal, there is usually a formal pattern (heh - word overload! let's say "template") for their description. 由于模式有点理论和形式,因此通常会有一个正式的模式(heh - word overload!让我们说“模板”)来描述它们。 Such a template may include: 这样的模板可能包括:

  • Name 名称
  • Effect achieved 效果达到了
  • Rationale 合理
  • Restrictions and Limitations 限制和限制
  • How to do it 怎么做

Idioms are something much lower-level, and usually operate at the language level. 习语是低级的,通常在语言水平上运作。 Example: 例:

*dst++ = *src++

in C copies a data element from src to dst while incrementing the pointers to both; 在C中将数据元素从src复制到dst同时将指针递增到两者; it's usually done in a loop. 它通常是在循环中完成的。 Obviously, you won't see this idiom in Java or Object Pascal. 显然,你不会在Java或Object Pascal中看到这个习惯用法。

while <INFILE> { print chomp; }

is (roughly quoted from memory) a Perl idiom for looping over an input file and printing out all lines in the file. 是(粗略地从内存中引用)一个Perl习惯用法,用于循环输入文件并打印出文件中的所有行。 There's a lot of implicit variable use in that statement. 在该语句中有很多隐式变量使用。 Again, you won't see this particular syntax anywhere but in Perl; 同样,除了在Perl中,你不会在任何地方看到这种特殊的语法; but an old Perl hacker will take a quick look at the statement and immediately recognize what you're doing. 但是一位老Perl黑客会快速查看声明并立即认出你在做什么。

Contrary to the idea that patterns are language agnostic, both Paul Graham and Peter Norvig have suggested that the need to use a pattern is a sign that your language is missing a feature. 与模式与语言无关的观点相反, Paul GrahamPeter Norvig都认为使用模式的必要性表明您的语言缺少某个功能。 (Visitor Pattern is often singled out as the most glaring example of this.) (访客模式经常被挑选出来作为最明显的例子。)

I generally think the main difference between "patterns" and "idioms" to be one of size. 我一般认为“模式”和“习语”之间的主要区别是大小。 An idiom is something small, like "use an interface for the type of a variable that holds a collection" while Patterns tend to be larger. 习语很小,比如“使用包含集合的变量类型的接口”,而模式往往更大。 I think the smallness of idioms does mean that they're more often language specific (the example I just gave was a Java idiom), but I don't think of that as their defining characteristic. 我认为习语的小巧确实意味着它们更多地是语言特定的(我刚才给出的例子是Java习语),但我并不认为这是他们的定义特征。

Since if you put 5 programmers in a room they will probably not even agree on what things are patterns, there's no real "right answer" to this. 因为如果你把5个程序员放在一个房间里,他们甚至可能不会就什么是模式达成一致,对此没有真正的“正确答案”。

One opinion that I've heard once and really liked (though can't for the life of me recall the source), is that idioms are things that should probably be in your language or there is some language that has them. 我曾经听过一次并且非常喜欢的一种观点(虽然不能让我的生活回想起来源)是,成语是应该用你的语言或者有一些语言来存在的。 Conversely, they are tricks that we use because our language doesn't offer a direct primitive for them. 相反,它们是我们使用的技巧,因为我们的语言不能为它们提供直接的原语。 For instance, there's no singleton in Java, but we can mimic it by hiding the constructor and offering a getInstance method. 例如,Java中没有单例,但我们可以通过隐藏构造函数并提供getInstance方法来模仿它。

Patterns, on the other hand, are more language agnostic (though they often refer to a specific paradigm). 另一方面,模式与语言无关(尽管它们通常指的是特定的范例)。 You may have some infrastructure to support them (eg, Spring for MVC), but they're not and not going to be language constructs, and yet you could need them in any language from that paradigm. 您可能有一些基础设施来支持它们(例如,Spring for MVC),但它们不是也不会成为语言结构,但您可能需要使用该范例中的任何语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM