什么时候设计模式是问题而不是解决方案?[英] When are design patterns the problem instead of the solution?

本文是小编为大家收集整理的关于什么时候设计模式是问题而不是解决方案?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我从来没有从事需要使用设计模式的软件.根据保罗·格雷厄姆(Paul Graham)的 nerds的复仇文章,设计模式表明没有足够的抽象.

直接引用他,"例如,在OO世界中,您听到了关于"模式"的很多交易.我想知道这些模式是否有时不是案例(C),人类编译器的证据.当我在程序中看到模式时,我认为这是麻烦的标志.程序的形状应仅反映其需要解决的问题.至少对我来说,代码中的任何其他规律性都是一个标志,我正在使用抽象 不够强大 - 通常我要手工生成一些我需要写的宏的扩展."

我只是想知道每个人是否认为设计模式过度使用,并且是您的代码中没有足够抽象的症状.

推荐答案

我不认为模式本身就是问题,而是开发人员可以学习模式,然后过度应用它们,或以非常不合适的方式应用它们的事实.

使用模式是经验丰富的程序员自然学习的东西.您已经解决了一些问题X多次,您知道哪种方法有效,您使用该方法,因为您的技能和经验告诉您这是合适的.那是一个模式,没关系.

但是,对于一个不太熟练的程序员来说,找到一种做事的方法并试图将他们遇到的所有问题塞入该模具中,这是可能的,因为他们不知道其他任何方法.那也是一种模式,这是邪恶的.

其他推荐答案

来找人,请阅读整个报价,仔细阅读.甚至更好,阅读论文.

保罗·格雷厄姆(Paul Graham)批评了类似C的语言,因为他们没有提供足够的抽象手段.在论文的范围内,他对模式的批评是事后的想法,或者是他的主要论点的案例.他的理由是这样的:

使用常用策略来解决反复出现的问题是合乎逻辑的.用真正抽象的语言,可以将这些策略形式化并将其放入图书馆.每当您需要使用它们时,您只会#cude它们,实例化,扩展它们或其他任何内容.相比之下,类似C的语言并不能提供必要的抽象手段.存在类似"模式"之类的事实证明了这一点.模式是一种常见的策略,无法用库代码表示,因此必须在每次应用时都要表达编写.

保罗·格雷厄姆(Paul Graham)并不认为模式是邪恶的.它们是提供抽象手段的语言的症状.在这方面,他几乎肯定是正确的.因此,我们是否应该使用不同的语言,当然是另一项讨论.

另一方面,问题的原始海报是错误的:模式不是"代码中没有足够的抽象的症状",而是语言中没有足够的抽象手段的症状.

其他推荐答案

模式实际上只是描述事物工作方式的一种方式.这是对它们进行分类的一种方式.有些程序过度使用它们吗?当然.拥有模式的最大优点是,通过将某些内容分类为或那样,每个人都在同一页面上(假设他们具有了解正在谈论的知识水平.).当您拥有具有10,000行代码的系统时,有必要能够快速确定某事的工作方式.

这是否意味着您应该始终使用模式,不.这将导致问题迫使事情进行分类,但您也不应该回避它们.

本文地址:https://www.itbaoku.cn/post/627721.html

问题描述

I’ve never worked on software where I needed to use design patterns. According to Paul Graham’s Revenge of the Nerds essay, design patterns are a sign of not enough abstraction.

To quote him directly, “For example, in the OO world you hear a good deal about "patterns". I wonder if these patterns are not sometimes evidence of case (c), the human compiler, at work. When I see patterns in my programs, I consider it a sign of trouble. The shape of a program should reflect only the problem it needs to solve. Any other regularity in the code is a sign, to me at least, that I'm using abstractions that aren't powerful enough-- often that I'm generating by hand the expansions of some macro that I need to write.”

I was just wondering if everyone thought design patterns are overused and are symptoms of not having enough abstraction in your code.

推荐答案

I don't think the patterns per se are the problem, but rather the fact that developers can learn patterns and then overapply them, or apply them in ways that are wildly inappropriate.

The use of patterns is something that experienced programmers just learn naturally. You've solved some problem X many times, you know what approach works, you use that approach because your skill and experience tell you it's appropriate. That's a pattern, and it's okay.

But it's equally possible for a programmer who's less skilled to find one way to do things and try to cram every problem they come across into that mold, because they don't know any other way. That's a pattern too, and it's evil.

其他推荐答案

Come on folks, please read the whole quote, read it carefully. Or even better, read the essay.

Paul Graham criticizes, among other things, C-like languages for not providing adequate means of abstraction. Within the scope of the essay his criticism of patterns is an afterthought, or rather a case-in-point for his main argument. His reasoning goes like this:

It is logical to use common strategies to solve recurring problems. In really abstract languages it is possible to formalize those strategies and put them into a library. Whenever you need to use them, you merely #include them, instantiate them, expand them, or whatever. C-like languages in contrast do not provide the necessary means of abstraction. That is witnessed by the fact that there exists something like "patterns". A pattern is such a common strategy that cannot be expressed by library code and thus has to be expressively written every time it is applied.

Paul Graham does not think that patterns are evil by themselves. They are a symptom of languages that fall short in providing means of abstraction. In that respect he is almost certainly right. Whether we should use different languages because of that, is of course another discussion.

The original poster of the question, on the other hand, is wrong: Patterns are not "symptoms of not having not enough abstraction in your code", but are symptoms of having not enough means of abstraction in your language.

其他推荐答案

Patterns are really just a way of describing how things work. It is a way of classifying them. Are there some programs that overuse them? Sure. The biggest advantage of having patterns is that by classifying something as this or that, everyone is on the same page (assuming they have the level of knowledge to know what is being talked about.). When you have a system with 10,000 of lines of code, it becomes necessary to be able to quickly determine how something is going to work.

Does this mean that you should always use patterns, no. That will lead to problems to force things into a classification, but you shouldn't shy away from them either.