TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Design patterns let you apply existing solutions to your code

53 点作者 skellertor超过 3 年前

18 条评论

vblanco超过 3 年前
I believe thinking of design patterns as something you &quot;use&quot; is one of the biggest mistakes being marketed to new programmers. A lot of them are niche use or only really exist to cover lack of features in languages, such as the Singleton pattern that has no real use in Cpp because you can just put a global pointer instead. It makes people think more about the code than about the problem they are trying to solve to begin with and often ends with a lot more code than is needed to solve the problem for no real good reason. Ive seen a lot of young programmers being mislead considerably by articles like this, including myself.<p>I see design patterns books and articles as more of a bestiary than a toolkit. Their purpose is not to be a repository of prebuilt solutions (because patterns dont solve anything by themselves). Their main purpose is to give names to a good amount of very common code patterns so you can communicate about it with other programmers.
评论 #28876348 未加载
评论 #28862412 未加载
BiteCode_dev超过 3 年前
Nah, design patterns are mostly communication tools.<p>When you name something &quot;factory&quot;, when some doc mentions &quot;dependency injection&quot;, when you import a &quot;register&quot;, when you talk with a colleague about a &quot;decodator&quot;, if you know the name, you already know what it does, and what the API will probably look like.<p>I say probably, because we don&#x27;t have a format definition of all design patterns, nor are they all recognized as such. For for the ones that are and we do, we don&#x27;t all agree. Not to mention you must adapt them to problems and languages.<p>So sure, when you code and you encounter a problem, you may use a design pattern. But you don&#x27;t explicitly do so. Just like a chess master doesn&#x27;t chose a move by thinking &quot;I&#x27;m going to use x&quot;, they just decide on it, and also adapt it to the situation.<p>I never think about design patterns when I code, only when I name the code, I write the doc, and talk about it. When I communicate.<p>If you read a book about design patterns, they will make no sense to you. You will probably misuse them. But if you look at real life source codes from other devs, or yours again and again, you realize some problems come back often, and a lot of people use similar solutions. Then you look it up: bingo, it has a name.<p>That&#x27;s how it works, not the other way around. It&#x27;s formalization of real life, not theory to apply.
评论 #28863518 未加载
jpm48超过 3 年前
I always give my students these to read when I teach design patterns :-) <a href="https:&#x2F;&#x2F;realtimecollisiondetection.net&#x2F;blog&#x2F;?p=44" rel="nofollow">https:&#x2F;&#x2F;realtimecollisiondetection.net&#x2F;blog&#x2F;?p=44</a> <a href="http:&#x2F;&#x2F;realtimecollisiondetection.net&#x2F;blog&#x2F;?p=81" rel="nofollow">http:&#x2F;&#x2F;realtimecollisiondetection.net&#x2F;blog&#x2F;?p=81</a> &quot;The “Design Patterns” book is one of the worst programming books ever. Yes, really. I’m 100% dead serious when I say that I think it has set (and will continue to set) the progress of software development back by decades.&quot;
评论 #28862852 未加载
评论 #28866512 未加载
jordwest超过 3 年前
The problem with design patterns is that it&#x27;s far too easy to &quot;apply existing solutions&quot; to problems that don&#x27;t exist.<p>Design patterns can definitely be useful, but only when used sparingly and if they really are the simplest solution to the problem. When applied just for the sake of &quot;best practices&quot; without really looking at the problem, all they do is create a new problem.
评论 #28861808 未加载
评论 #28862214 未加载
评论 #28861549 未加载
评论 #28869178 未加载
evercast超过 3 年前
When looking at the GoF design patterns, I believe one should also note that they were created not only to solve a bunch of design problems, but also to deal with the limitations of the programming languages back in the day. For example, if you learn about them in the Java context, they make perfect sense as they are well aligned with the language features required to implement them. Try however putting them in Python context and they often result in unnecessary complexity.<p>You can actually see this in the article. The Python examples are awful (not very idiomatic). This is just Java written in Python, even with non-Pythonic camelCase naming.
评论 #28873932 未加载
kubb超过 3 年前
In software engineering certain ideas that sound neat can undeservingly gain a lot of popularity and endorsment. OO and design patterns are among those ideas. The promise is tempting - we&#x27;ll give you a template to solve problems, and that will prepare you for most of the situatuons that you encounter. This appeals to inexperienced programmers who don&#x27;t know how to approach real-world tasks.<p>The idea will probably remain popular as long as programming is a thing, with an ecosystem of blog posts, tech talks and expert books to grant it credibility.<p>But let me tell you this: there are principal engineers at FAANGs that wouldn&#x27;t be able to name more than one design pattern (the one being Singleton).<p>DP might help in your career, but only if your bosses also believe in them. Many things in our profession are based purely on belief.
评论 #28874042 未加载
dunefox超过 3 年前
&quot;Design patterns are bug reports against your programming language.&quot; - Peter Norvig, <a href="https:&#x2F;&#x2F;norvig.com&#x2F;design-patterns&#x2F;" rel="nofollow">https:&#x2F;&#x2F;norvig.com&#x2F;design-patterns&#x2F;</a><p>I have to say that I agree with this. In my experience design patterns in Java lead to overly complicated, over-engineered code, think abstract factory factory factory. I&#x27;ve seen it and had it described as &quot;best practice&quot;, decoupling, design patterns, etc.
kelseyr超过 3 年前
I&#x27;ve seen too many examples of code at work where you can tell a developer has just come back from a training course on design patterns. It&#x27;s usually an overly complicated solution &#x2F; &#x27;pattern&#x27; that has been applied to solve a simple problem, because you&#x27;ve learnt these things right, so you should use them where you can. Rather than sticking to the simplest solution. Less code == easier to understand (for yourself _and_ others that will have to maintain your code) == less chance of bugs slipping in.
marcos100超过 3 年前
The authors of &quot;Design Patterns&quot; didn&#x27;t create anything new, they just documented common patterns used in the industry and gave them a name. So, when you say &quot;I used X pattern here&quot;, I can have a high level architecture of the code in my mind when navigating it and how it&#x27;s supposed to work.<p>You should read the title as &quot;Design Patterns for Java&quot;. You can try to implement the patterns in other languages and it may work but, if it doesn&#x27;t, that doesn&#x27;t mean the book is useless, it just doesn&#x27;t fit your language of choice.<p>I believe someone would&#x27;ve written a book similar to &quot;Design Patterns&quot; at some point in time. It just happened to be the GoF.<p>You can say whatever you want, but today we all use patterns to talk about software. It may or may not be thanks to the book. We&#x27;ll never know.
jmartinpetersen超过 3 年前
It starts with the Singleton and vaguely hints that it has some drawbacks. Why start with that, then? And why not flat our recognise that it is understood more as an anti-pattern these days?<p>I don&#x27;t get blogs like these. This story has been told a million times with more or less the same content and structure.
评论 #28861584 未加载
评论 #28861781 未加载
评论 #28861758 未加载
galaxyLogic超过 3 年前
I&#x27;ve recently programmed quite a lot using &#x27;Visitor&#x27;. But looking at my code a few months later it seemed hard to understand. Why? I think it is because the abstraction level is too high. The names in the code don&#x27;t refer to anything that has to do with the problem the code is trying to solve.<p>For instance I needed to traverse some directories and do something with the files and subdirs in them. So I used my parametric Visitor class.<p>But when I now read that code it has calls to visit() etc. and when I debug the code it never mentions &#x27;files&#x27; and &#x27;folders&#x27;. That has been abstracted away into parameters. And that makes it difficult to understand what the code is actually doing, what it is accomplishing.<p>So, I&#x27;ve found it gets easier if I rewrite the code to use the vocabulary of the problem domain. It still follows the same (Visitor-) &quot;pattern&quot;. The pattern is there but code does not use the vocabulary of the pattern, but of the concrete things it is dealing with.<p>And maybe that&#x27;s why Design Patterns are important. Not as a code-library but as explanations of &quot;patterns&quot; that are useful when writing software.
评论 #28874872 未加载
zabil超过 3 年前
Good design and OOP avoids using GoF patterns while coding for many reasons. One is that they do not map well to the domain and the other is they are difficult to TDD or test. They are really tough to use too if you plan to publish your lib as an API.<p>The reason GoF got so popular is because for anyone struggling with OO principles it was easy to understand and apply. I hope for a better way to teach design by not using patterns.
评论 #28864815 未加载
snidane超过 3 年前
(design) patterns are an initial step in the process of automating the automation.<p>You, the automation engineer (programmer) notice you are using the same way of solving a thing and the code of the solution at all occurrences looks somewhat similar. Well, we are no longer writing in machine code and have subroutines and functions. So you go ahead and turn the pattern into a function of your language has them. Sometimes a function is not enough, but fortunately we have macros. At least some languages do. For the unfortunate ones with less powerful languages you have to keep it at the pattern stage and never evolve to reduce the boilerplate or automate your job away.<p>For this reason design patterns are often called the &quot;missing language features&quot;. Which you can never understand until you have touched the powerful languages with metaprogramming capabilities.<p>Now, there are situations in which design patterns have to be used because they are applied at a cross section between systems. There is no single language encompassing your entire code, but many languages for many systems. Unless you go crazy with string templating (ie. inventing a new poor man&#x27;s language), you will not be able to move beyond the pattern stage. On the other hand, a repeated instance of the problem between systems suggests there could exist a language that many people would use. The problem was repeated after all.<p>This usually gets resolved by a simple system integration engine, which is operated through some json, xml or yaml configuration file. Eventually there comes a need to introduce variables, file includes, loops, etc.<p><a href="http:&#x2F;&#x2F;mikehadlow.blogspot.com&#x2F;2012&#x2F;05&#x2F;configuration-complexity-clock.html?m=1" rel="nofollow">http:&#x2F;&#x2F;mikehadlow.blogspot.com&#x2F;2012&#x2F;05&#x2F;configuration-complex...</a><p>People turn to become &#x27;advanced yaml engineers&#x27;, like now in the k8s world, for what perhaps could be managed by programming language.<p>After many frustrations, eventually a new special purpose language will be built for it.<p>Long lasting design patterns in the codebase are pointing at missing language features. Or at a new programming language at the horizon.
trhway超过 3 年前
design patterns create impression that it is possible to and naturally encourage to piecemeal substitute the system level understanding with the understanding of the local behavior. I&#x27;ve seen such software projects many times - beautiful patterns everywhere, you can teach students using that project, and the only &quot;minor&quot; defect is that the software is extremely buggy and slow, the better patterned&#x2F;architected - the slower&amp;buggier.<p>No wonder it is so beloved by all those enterprise architects - it allows to &quot;architect&quot; without actually architecting, like building a bridge without an overall bridge plan and calculations, only with the plans of its components and just piling, &quot;injecting&quot; and smashing these components together.
moon_other超过 3 年前
The design patterns as defined by the book &quot;Design Patterns&quot; seem to be closely-coupled to the implementation language. This presents them more as cookbook recipes and less as patterns.<p>It would help if the design patterns could be defined per language and written in the language&#x27;s own idioms.
anon9001超过 3 年前
&gt; Design patterns are solutions to commonly occurring design problems in developing flexible software using object-oriented programming.<p>This completely misses the point. You don&#x27;t &quot;pick a pattern&quot; and choose to apply it.<p>Design patterns are a way to take your best solution and describe how it works to other programmers.<p>For example you might say something like: &quot;We used a singleton here because we can barely manage to get one instance working correctly and never intend to write tests&quot;.
评论 #28862598 未加载
rvense超过 3 年前
They also let you import existing problems into your code.
antman超过 3 年前
A nice presentation, but which are the most common design patterns? Are those in the article? I would also rank high e.g. Observer. Also some toy real world examples are common in those sort of presentations for a more complete understanding. In relation to other articles this gives better intuitions on where they are used.