Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 5th, 2007, 03:46 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default Design Patterns

Hi folks,

I just wanted to start a general discussion on design patterns and get people's thoughts on them. I have been reading the "Head First Design Patterns" book (sorry Wrox, it's published by O'Reilly) and have been trying to better understand patterns. I understand what the book is talking about and understand how the patterns apply to the examples they provide.

One of the things I've had a lot of difficulty with is trying to figure out how the patterns can fit into my software architecture and how to determine *when* and *where* I should use a certain pattern. I think the significant problem is that so much of the work I've done in software has been primarily web based applications that essentially do nothing more than data entry and retrieval. It seems like patterns can be applied much easier to designs of programs with more statefulness and business processing. I have had a hard time finding an application for patterns when doing simple SELECTs and UPDATEs to and from a database in the stateless environment of a web application.

I'm interested to hear other opinions this topic. What kinds of things have you learned about patterns and how have you been able to apply them to your applications?

-Peter
__________________
-Peter
compiledthoughts.com
twitter/peterlanoie
Reply With Quote
  #2 (permalink)  
Old January 5th, 2007, 06:09 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A good start is to also read the GOF book: Design Patterns: Elements of Reusable Object-Oriented Software.

And now to begin the discussion:
IMO the real value of the GOF book was to provide a way to define and discuss desgin patterns, not the patterns themselves. In other words, the idea of design patterns itself is valuable, and having a way to communicate to each other about patterns is useful. The 23 (or however many) patterns in the original GOF book, and the other books that mostly re-explain these patterns are all (or mostly) useful in the right context - but those are just a few patterns out of hundreds (or thousands) that probably could be identified if we took all the code in all the applications and looked for common solutions to typical problems. The point is - those 23 patterns are a good set for learning about patterns, and they are useful (I use many of them as some of my standard design tools), but they aren't a sort of holy grail or "be all end all".

What I often see, both in working on software project teams and in reading blogs and forums and other postings on the web, is that people who have read a design patterns book often start to try to apply patterns without really understanding the principles of object oriented design. These principles provide the basis on which the patterns become possible. So, what are these principles? The best resource I have found on these principles is the book by Robert Martin: Agile Software Development Principles, Patterns, and Practices. If you can learn what this book teaches about the Principles of Class and Package design, then the Patterns will become much easier to use. In some ways, this is a very advanced book, but it is all digestable by the average programmer, and even though "patterns" books are sexier I believe the Robert Martin book should be a prerequisite to studying patterns books.

So. Back to the topic at hand:
"When and Where to use a certain pattern". A pattern is an identified solution to a (usually) common problem. If you have the problem then you can most likely apply the pattern. Whether the pattern is the best solution isn't guaranteed: It is merely one observed solution.

One way to get started with patterns is to identify a particularly troubling part of your code in a project you are working on - and see if it fits one of the patterns. For example - when working in legacy code I often find there are several classes that do slightly different things but it would be useful for me to be use polymorphically - and I could if they had all had the same base class or interface, but they don't. Since these classes are already in use by numerous other objects (the typical problem of being tightly coupled) I can't change them or I risk breaking all kinds of working code. So, the problem is: How can I polymorphically use these classes (that are already working, and proven to be stable) without having to re-write them or change them? What I want is a way to use these objects with a common interface - but it doesn't exist. So, if I didn't already know a solution to this I would look through the GOF book, or my notes on it, to see if this problem has been solved by one of the pattern(s). So... I'll leave that for you to decide, and then I'll tell you one pattern from the books that is commonly used for this purpose. Is that fair?

One side note: Since this is a .NET forum, then it might be useful to point out that you will see many of the 23 patterns used somewhere or other in the .NET framework classes. Things like the template pattern, factories of various kinds, iterators, and so on. Once you have started to learn the patterns, you will start noticing how they are used in the framework.

Woody Z
http://www.learntoprogramnow.com
Reply With Quote
  #3 (permalink)  
Old January 8th, 2007, 10:42 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Woody,

Thanks for a great post (extra points for correct capitalization)!

Quote:
quote:IMO the real value of the GOF book was to provide a way to define and discuss desgin patterns, not the patterns themselves.
Very insightful. It's interesting. I had someone hand me a design pattern document template with the instructions "can you write a pattern about X that you built". Well, X was a particular control I'd migrated from one environment to another and wasn't something (IMO) that was applicable to a pattern. He was really asking for a post-construction design spec. My point is that he didn't really understand at the time what the idea of a design pattern was.

Quote:
quote:One way to get started with patterns is to identify a particularly troubling part of your code in a project you are working on - and see if it fits one of the patterns.
I have had many cases where, as in your example, I've had existing code that I suddenly recognize would be a very good candidate for a pattern solution. But what about when I have a blank slate that I want to fill with well-designed, pattern influenced code? It doesn't seem to make sense to me to wait until I've constructed "bad" code to refactor it with patterns.

Quote:
quote:So... I'll leave that for you to decide, and then I'll tell you one pattern from the books that is commonly used for this purpose. Is that fair?
Do I smell adapter?

Quote:
quote:One side note: Since this is a .NET forum, then it might be useful to point out that you will see many of the 23 patterns used somewhere or other in the .NET framework classes.
A very good point I meant to raise initially. I've been trying to get my head around the command pattern but am having a little more difficulty with it than I did with the others. I imagine there are classes in the framework that apply the command pattern. Dare I guess that the xxxCommand classes in the System.Data.* namespaces are examples?

-Peter
Reply With Quote
  #4 (permalink)  
Old January 8th, 2007, 04:10 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

First of all, I don't consider myself a pattern expert, but I have been trying to apply patterns and refactor to patterns for 8 or so years.

Quote:
quote:Thanks for a great post (extra points for correct capitalization)!
Thanks for the extra points, I will put those to good use.

Quote:
quote:... But what about when I have a blank slate that I want to fill with well-designed, pattern influenced code? It doesn't seem to make sense to me to wait until I've constructed "bad" code to refactor it with patterns.
This is a fair question, and my practice is to use patterns I know well whenever I am working in green-field code and have a good feeling that a particular pattern will make things easier. But, I also try to follow the XP/Agile practice of doing the simplest thing that could possibly work. Similarly, like the Marines, I look for the 70% solution - It's better to decide quickly on an imperfect plan than to roll out a perfect plan when it's too late. To paraphrase that, I don't go looking for patterns until I have a problem. I used to say "If something is worth doing, its worth doing poorly" - but people didn't seem to understand that. Still - I like the sentiment which I think is one part of the XP saying (from Kent Beck, I think): Make it run, make it right, make it fast, make it small. Perhaps I am covering too much here, but these could be considered the steps in an iterative process. All of these are useful, but after the first two steps you could be delivering useful code. If you find you have performance problem, you can move on to the Make it Fast step... and so on.

Quote:
quote:Do I smell adapter?
Good work. That is the one I would say is a match, as well. So one thing to see here is that it doesn't do much good to try to use an Adapter if you don't have a problem that an Adapter would fix.

Quote:
quote:...I've been trying to get my head around the command pattern but am having a little more difficulty with it than I did with the others. I imagine there are classes in the framework that apply the command pattern. Dare I guess that the xxxCommand classes in the System.Data.* namespaces are examples?
Hm. I imagine we can probably find some aspects of the data command objects that follow this pattern, but it probably is more just the name that is similar rather than the concept. There are some features of C# that imploy a command pattern thanks to delegates - for example, menu items on a standard menu use a command pattern. This is a simple command pattern exapmle.

There are a lot of books on Design Patterns - but there are a couple that relate directly to C#. One is the Design Patterns in C# by Steve Metsker. If you can afford it, or can get it cheap used somewhere, it might be helpful to get this one too.

Woody Z
http://www.learntoprogramnow.com
Reply With Quote
  #5 (permalink)  
Old January 12th, 2007, 01:34 AM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was at the book store today and took a look at that "Head First Design Patterns" book. Not too bad. I might try to get my hands on a copy of it if I can find it used at half.com or somewhere like that.

Woody Z
http://www.learntoprogramnow.com
Reply With Quote
  #6 (permalink)  
Old February 21st, 2007, 01:56 PM
Friend of Wrox
 
Join Date: May 2006
Location: San Diego, CA, USA.
Posts: 643
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One thing about patterns is that they can be used "up-front" when you are designing your system, or later on when you start seeing problems arise. If you follow a purely test-driven methodology, you might not want to design up-front, but that doesn't mean that you have to ignore patterns until a problem becomes apparent. In other words, the better you understand design patterns the earlier in your coding you'll be able to see a situation developing that can be helped by introducing a known design pattern. Test driven development itself almost requires that you code in a manner that will cause your code to gravitate towards patterns. In other, other words, the qualities that result from TDD are "in-tune" with the qualities of the principles of OOP (ala Robert Martin), the qualities of Design Patterns, and the qualities that result from good refactoring.

Woody Z
http://www.learntoprogramnow.com
How to use a forum to help solve problems
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design patterns for web design ceadge HTML Code Clinic 0 June 19th, 2006 11:26 AM
Professional Design patterns in c# preticul Wrox Book Feedback 0 February 7th, 2005 06:36 PM
Java Design issue with UML and Design Patterns the_logical_way Apache Tomcat 0 May 31st, 2004 04:02 AM
Design Patterns Indian Ocean J2EE 0 September 25th, 2003 03:18 PM
DESIGN Patterns rahul1978 BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 3 August 31st, 2003 02:02 PM



All times are GMT -4. The time now is 05:59 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.