我已经阅读了大约4-5本关于设计模式的书籍,但我仍然觉得我在设计模式方面还没有接近中级水平?

我应该如何学习设计模式?

有关于设计模式的好书吗?

我知道这只会来的经验,但必须有一些方法来掌握这些?


当前回答

设计模式只是工具——有点像库函数。如果你知道它们和它们的近似函数,你可以在需要的时候从书中找到它们。

设计模式并没有什么神奇之处,任何优秀的程序员都能在任何书籍问世之前自己找出90%的设计模式。在大多数情况下,我认为这些书在简单地为各种模式定义名称方面最有用,这样我们就可以更容易地讨论它们。

其他回答

练习,练习,再练习。

你可能读了很多年的关于演奏大提琴的书,但仍然不会用弓演奏乐器,也不会演奏出任何听起来像音乐的东西。

设计模式被认为是一个高级问题;只有当你有必要的经验来认识到它们是有用的,它才与你相关。你能认识到它们的有用是件好事,但除非你亲眼目睹过它们的应用场景,否则几乎不可能理解它们的真正价值。

当你在别人的代码中识别出设计模式,或者在设计阶段识别出一个与模式很适合的问题时,它们就会变得有用;然后检查形式模式,检查问题,确定它们之间的增量是什么,这说明了模式和问题的什么。

这和编码是一样的;K&R可能是C的“圣经”,但从头到尾读几遍并不能给人实际的体验;经验是不可替代的。

我认为您需要检查一些您作为开发人员遇到过的问题,当您因为另一个设计更改而不得不第十次修改代码时,您会感到非常紧张。你可能有一个项目清单,你觉得有很多返工和痛苦。

From that list you can derive the scenarios that the Design Patterns intend to solve. Has there been a time where you needed to perform the same series of actions on different sets of data? Will you need to be able to future capability to an application but want to avoid reworking all your logic for existing classes? Start with those scenarios and return to the catalog of patterns and their respective problems they are supposed to solve. You are likely to see some matches between the GoF and your library of projects.

The way I learned design patterns is by writing lots of really terrible software. When I was about 12, I have no idea what was good or bad. I just wrote piles of spaghetti code. Over the next 10 years or so, I learned from my mistakes. I discovered what worked and what didn't. I independently invented most of the common design patterns, so when I first heard what design patterns were, I was very excited to learn about them, then very disappointed that it was just a collection of names for things that I already knew intuitively. (that joke about teaching yourself C++ in 10 years isn't actually a joke)

这个故事的寓意是:编写大量的代码。就像别人说的,练习,练习,再练习。我认为,在您了解当前的设计为什么不好并寻找更好的方法之前,您不会很好地了解在哪里应用各种设计模式。设计模式书应该为您提供完善的解决方案和与其他开发人员讨论的通用术语,而不是对您不理解的问题的粘贴解决方案。

我领导了一些设计模式讨论小组(我们的网站),并阅读了5或6本模式书籍。我建议从《头部优先设计模式》一书开始,参加或发起一个讨论小组。《Head First》一开始可能看起来有点像Hasboro,但大多数人在读了一两章后就喜欢上了它。

Use the outstanding resource - Joshua Kereivisky's A Learning Guide to Design Patterns for the pattern ordering and to help your discussion group. Out of experience the one change I suggest to the ordering is to put Strategy first. Most of today's developers have experienced some good or bad incarnation of a Factory, so starting with Factory can lead to a lot of conversation and confusion about the pattern.This tends to take focus off how to study and learn patterns which is pretty essential at that first meeting.

你试过《四人帮》这本书吗?

设计模式:可重用面向对象软件的元素