学习/实施设计模式(适用于初学者)

我是一个困惑的新手和业余程序员试图抓住这一点,所以原谅我,如果我的问题是有点偏离或没有太大的意义。

我看到很多关于 SO 的问题都围绕着设计模式的使用,我想知道是否有人有很好的资源来学习和实现设计模式?我理解大概的概念,也知道如何/何时使用其中的一些(单例,工厂方法) ,但是我知道我错过了。

(以防万一,我最喜欢的语言是 C # ,但我可以从其他语言的例子中学习)

18841 次浏览

Head First Design Patterns

alt text

and the Design Pattern Wikipedia page are the best resources for beginners. FluffyCat is another good, free online resource for design patterns in both Java and PHP.

The Gang of Four book is where to go afterward, but it's fairly advanced, so I'd wait until you have a pretty firm grasp from the other resources.

Patterns of Enterprise Application Architecture (Hardcover) by Martin Fowler

Refactoring to Patterns (Hardcover) by Joshua Kerievsky

Continuous Integration: Improving Software Quality and Reducing Risk (Paperback) by Paul Duval et.al.

Beyond Software Architecture: Creating and Sustaining Winning Solutions (Paperback) by Luke Hohmann

I'd add that Design Patterns book from the "Gang of four" is a bible for anyone who is seriously interested in design patterns.

Design Patterns      
(source: Amazon)

Design patterns are like any library function, read about them, then when a problem comes up, the design pattern will be in your "Toolchest". There are many design patterns books all patterned after the original "Gang of four" design patters.

For any programmers, I think that and the Refactoring book by Fowler are the absolute minimal requirements.

It does not make too much sense to me for someone with very little experience to delve too far deeply into design patterns. It is great to know they exist, but at this point you should focus more on other things rather than just learning about design patterns.

They are useful in context of a problem - as a concept for a new/beginner developer they are really not too much practical value aside form knowing that you should make use of them when and where you can.

EDIT To clarify - many design patterns are a result of problems found in some domains. A new programmer can hardly be expected (IMO) to know the design pattern(s) to use for some set of problems. Just as we get a smattering of algorithms in CS studies, we need an understanding of the things we can do with patterns and their benefits, but when a person is still building hello world or discovering stl there is not much practical need for design patterns. Patterns are great. But they are not the silver bullet.

(Neither was CASE (tools), neither is/was UML, neither is SCRUM, neither is TDD, nor STL, nor Java, nor XML, etc. ) These are all just aspects of our profession and to treat these topics as the second coming is naive.

For websites, a very good site is http://ajaxpatterns.org, from one of the developers of the ajaxian website

The original Design Patterns book is a must-read for all programmers.

It is an excellent book on every level: layout, clarity, insight, depth. It's one of those great books that you first read cover-to-cover, and then use as a reference until you literally know it inside out.

You could start by the Wikipedia page, but treat yourself with the great book too.

My tip:

Read a lot about the patterns from different sources.

Trying to force as many patterns as possible into all the code you're writing as this point is not going to give good results. Instead, let the information rest inside your brain for a while (read: months).

Suddenly you'll find yourself stumbling upon a problem or piece of code and you'll vaguely remember that you've seen something that might work as a solution to this particular problem. Now is the time to look up the details on the pattern you are thinking of and try to apply it.

That's what has worked for me, anyway.

Bruce Eckel has a book on design patterns, although it's Java, it's like all of his books amazing. And the best thing is, they are free!

Applying UML and Patterns by Craig Larman. Start from the basic of analysis, design and uses a simple Case scenario. Introduces most of the basic patterns in a simple way.

I also suggest having a shufty at Refactoring to Patterns once you've read Head First Design Patterns.

Note: The code examples are in Java, but should be very similar to C# examples...

C# 3.0 Design Patterns for a C# perspective on design patterns.

alt text
(source: oreilly.com)

If you read about design patterns you'll notice that Java seems to have a few of them implemented.

Look at the source for any framework and you can glean info about design patterns. Personally I don't see them fitting in perfectly to any of my code, sometimes the examples in books and tutorials seem a little idealized, especially for the lone coder.

Design patterns aren't for lazy coders.

Design patterns are great for various reasons:

  1. gives you a starting for solving common problems.
  2. gives developers a vocabulary to talk about certain ways of solving problems in a very compact manner.
  3. when working with developers who know design patterns and you use design patterns in your solutions they will understand the solutions a lot faster.

But when your goal is just to learn design patterns I think you are missing the fundamentals. All design patterns are based on more common principles. High Cohesion, Low Coupling Open Closed Principle, DRY, Liskov Substitution Principle etc. For these fundamentals I would read the following books in this order:

  1. Head First Object-Oriented Analysis and Design (Head First) [ILLUSTRATED] (Paperback)
  2. Applying UML and Patterns (Hardcover)
  3. Agile Principles, Patterns, and Practices in C# (Robert C. Martin Series) (Hardcover)

After that you are ready for the basic gang of four design patterns

  1. Head First Design Patterns (Head First) [ILLUSTRATED] (Paperback)
  2. The BIBLE
  3. A nice website (don't buy anything, it is not worth it) http://dofactory.com/Patterns/Patterns.aspx (some implementations of this site are worth a discussion

The next step:

  1. Patterns of Enterprise Application Architecture (Addison-Wesley Signature Series) (Hardcover)
  2. The POSA books

And always remember : the pattern is not the goal !

Patterns comprise the high level vocabulary programmers use to talk about abstract design. If you are reusing an abstract solution, it is helpful to refer to it by name. If you invent a pattern, it is professional to do a little checking to make sure it hasn't already been given a name. If it has been named, then the description may be useful.

After you code even a tiny bit, you will notice yourself writing something similar to what you coded before. This is a pattern. Even if it is a tiny pattern, it is worth noticing. Is there a better pattern? Do you see certain tiny patterns cooperating together to solve a larger problem? Well next time, when you want to solve larger problem, the entire pattern comes into your mind as a single chunk. Fleshing out the detailed lines of code becomes mechanical.

The more you notice patterns, the easier programming becomes, and the more you will appreciate some of the biggest and best patterns worked out by other programmers. Try mastering the MVC pattern. One way or another, variations show up all over the place, even in tiny design decisions.

An introductory book that I found useful and well written is Design Patterns Explained by Alan Shalloway and James Trott (Addison Wesley).

alt text

Do not start from the Gang of Four book, for it is not an introductory book by any means.

Head First Design Patterns is a good one like others already noted. Besides this, of course the original book and C# Design Patterns. Also there are good websites already mentioned.

Besides self-learning, I seriously recommend starting or attending a pattern study group in your area. See A Learning Guide To Design Patterns for explanation and great order to study the patterns in. We did this and I can honestly say that I would not understand as much as I do now. A weekly meeting with other interested people keeps you surprisingly disciplined when learning something abstract like design patterns.

Happy studies!

I would recommend taking a look at Jean Paul Boodhoo's quintology (?) on Demystifying Design Patterns on DNRtv, urls supplied below. The videocasts touches on Singleton, Abstract Factory among others- the difference being that you can watch him code as he discusses the theory. Good to watch over lunch on a rainy weekday.

http://www.dnrtv.com/default.aspx?showNum=63 http://www.dnrtv.com/default.aspx?showNum=65 http://www.dnrtv.com/default.aspx?showNum=68 http://www.dnrtv.com/default.aspx?showNum=71 http://www.dnrtv.com/default.aspx?showNum=92

For me and my colleagues study Design Pattern that following on Pattern Study Group. They prepare a list of each pattern that we should learn in order and also have the opening questions that make more discussion in group.

Once you understand the concept, go through the Eclipse source code or design, lots of really good examples of these patterns (no surprise, Gamma was one of the designers).

An annotation to the above comments.

A Quick Reference For GOF Patterns

Here is a good place you can start dofactory.com/patterns/patterns.aspx - You can find link to each pattern, along with corresponding implementations.

How ever, remember that these are GOF patterns. You might need to read and understand advanced patterns as well, once you gain enough expertise in OOAD. Head First Design Patterns is a good start, and after making some progress, go with Martin Fowler's Enterprise Application Architecture Patterns.

Applying Design Patterns - The Thought Process

Another main aspect - Applying Design patterns is as important as just knowing them. Reading these articles might help you as well.

Applying Design Patterns Part I

Applying Design Patterns Part II

Hope this helps

I have found Design Pattern articles on this website really easy to understand

Design Patterns in C#

Design Patterns

This tutorial site contains following sub-sections

  • Intent of each design pattern
  • Real World Structure for the design pattern
  • A Problem statement
  • Detailed discussion on the problem
  • Checklist on how to arrive at a pattern
  • Rules of thumb while arriving at pattern.
  • Code snippets for the design pattern which includes C#, C++, Delphi, Java and PHP

This site also contains guide on Anti Patterns, UML and Refactoring.

Before spending money on books I would recommend Wikipedia's excellent design patterns page. Also for something different Google for "design pattern screencasts" or search for "design patterns" on YouTube. Getting the same information presented differently often helps the penny drop.

The Gang of Four book is the definitive text on the most well known patterns but is not that easy to read and with C++ examples not everyone's favourite.

The Head First Design Patterns text is far more accessible but only contains a subset of the Gang of Four patterns.

The most important thing is to understand where and why a particular pattern is useful. Afterwards search the web for implementation examples in the language of your choice and experiment until you "get it". Understand one pattern before moving on to the next. Everyone understands some patterns better than others (and there are hundreds of lesser known ones).

Just keep plugging away.