我已经阅读了大约4-5本关于设计模式的书籍,但我仍然觉得我在设计模式方面还没有接近中级水平?
我应该如何学习设计模式?
有关于设计模式的好书吗?
我知道这只会来的经验,但必须有一些方法来掌握这些?
我已经阅读了大约4-5本关于设计模式的书籍,但我仍然觉得我在设计模式方面还没有接近中级水平?
我应该如何学习设计模式?
有关于设计模式的好书吗?
我知道这只会来的经验,但必须有一些方法来掌握这些?
当前回答
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)
这个故事的寓意是:编写大量的代码。就像别人说的,练习,练习,再练习。我认为,在您了解当前的设计为什么不好并寻找更好的方法之前,您不会很好地了解在哪里应用各种设计模式。设计模式书应该为您提供完善的解决方案和与其他开发人员讨论的通用术语,而不是对您不理解的问题的粘贴解决方案。
其他回答
最好的方法就是用它们开始编码。设计模式是一个伟大的概念,仅仅通过阅读很难应用它们。从网上找到一些示例实现,然后围绕它们进行构建。
数据和对象工厂页面是一个很好的资源。他们会讲解这些模式,并给出概念上和现实世界中的例子。他们的参考资料也很棒。
对于这样一个老问题,我只有两分钱
有些人已经提到,实践和重构。我认为学习模式的正确顺序是:
学习测试驱动开发(TDD) 学习重构 学习模式
大多数人忽略了1,许多人相信他们可以做2,几乎每个人都直接去做3。
对我来说,提高软件技能的关键是学习TDD。这可能是一个痛苦而缓慢的编码过程,但首先编写测试肯定会让您对代码进行很多思考。如果一个类需要太多样板文件或者很容易崩溃,你很快就会注意到不好的味道
TDD的主要好处是你不再害怕重构你的代码,并迫使你编写高度独立和内聚的类。如果没有一套好的测试,触摸没有损坏的东西就太痛苦了。有了安全网,你将真正冒险对你的代码进行巨大的更改。这是你真正开始从实践中学习的时刻。
Now comes the point where you must read books about patterns, and to my opinion, it is a complete waste of time trying too hard. I only understood patterns really well after noticing I did something similar, or I could apply that to existing code. Without the safety tests, or habits of refactoring, I would have waited until a new project. The problem of using patterns in a fresh project is that you do not see how they impact or change a working code. I only understood a software pattern once I refactored my code into one of them, never when I introduced one fresh in my code.
I would think it is also difficult to study design patterns. You have to know more about OOP and some experiences with medium to big application development. For me, I study as a group of developers to make discussion. We follow A Learning Guide To Design Patterns that they have completed the patterns study. There are C# and JavaScript developers join together. It is fancy thing for me is the C# developer write codes in JavaScript and the JavaScript developer do the same thing for C# codes. After I leave a meeting I also research and read a few books at home to review. The better way to understand more and remember in my mind is to do blogging with examples in both C# and JavaScript in here http://tech.wowkhmer.com/category/Design-Patterns.aspx.
我建议首先在去了解每个设计模式之前请先了解模式的名称。此外,如果有人知道这个概念,请解释并给出一个例子,不仅是编程,而且是阅读世界。
例如:
工厂方法:
阅读世界:我只要给钱5美元、10美元或20美元,它就会生产出披萨,而不知道它是如何生产的,我只会得到一个小的、中等的或大的披萨,这取决于我投入的钱,这样我就可以吃或做任何事情。
编程:客户端只需将参数值$5,$10或$20传递给factory方法,它将返回Pizza对象。因此客户端可以使用该对象,而不知道它是如何处理的。
我不确定这能帮到你。这取决于与会者的知识水平。
你读过Allan Shalloway写的《设计模式解释》吗?
这本书与其他设计模式书籍有很大的不同,因为它并不是一个模式目录,而是主要介绍了一种分解问题空间的方法,可以很容易地映射到模式。
问题可以分解为两部分:共同的和不同的。一旦完成了这一步,我们就可以将常见的内容映射到接口,并将不同的内容映射到实现。从本质上讲,许多模式都属于这种“模式”。
例如,在策略模式中,常见的事物表示为策略的上下文,可变部分表示为具体的策略。
我发现这本书与其他模式书相比非常发人深省,对我来说,阅读电话簿的兴奋程度是一样的。
我认为您需要检查一些您作为开发人员遇到过的问题,当您因为另一个设计更改而不得不第十次修改代码时,您会感到非常紧张。你可能有一个项目清单,你觉得有很多返工和痛苦。
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.