What's with design patterns

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

What's with design patterns

Postby Live24x7 » Mon May 28, 2012 5:21 pm

I have finally taken the quantum leap from procedural to to OOP.

1) i am reading books on OOP
2 ) reading codes designed in OOP

Atleast they have started making some sense to me now, though not yet coded anything n OOP so far

Next, design patterns (the gang of four book) is something i have been influenced to explore.
I read a little on the web but could not make much sense of its practical usage.

My Question

1) Where does 'design patterns' fit in the entire OOP schema ?>

Are they necessary to know ?
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: What's with design patterns

Postby Christopher » Mon May 28, 2012 10:30 pm

Live24x7 wrote:1) Where does 'design patterns' fit in the entire OOP schema ?
They are simply Best Practice solutions to common problems. They are usually stated as a problem then a solution. They have been proven by the best and brightest over the years to be a successful way to solve these problems. Though the pattern concept is not OOP specific they are very much associated with OOP and are mostly OOP solutions.
Live24x7 wrote:Are they necessary to know ?
Yes, because they provide a common language to talk about software problems/solutions without needing very long-winded explanations. They form a ubiquitous language to talk about these complex subjects.
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12662
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What's with design patterns

Postby Live24x7 » Mon May 28, 2012 11:44 pm

Thanks a lot :)

I read yesterday that design patterns are to programming what scales are to music :)
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: What's with design patterns

Postby requinix » Tue May 29, 2012 1:43 am

Live24x7 wrote:I read yesterday that design patterns are to programming what scales are to music :)

Actually I'd say they're more like chords.

Scales are programming languages. They include a fairly definitive set of notes you can play, and playing different notes probably means you're doing something wrong. Occasionally you might use a couple different keys/scales in a single piece of music, but for the most part you're dealing with just one.
Chords are design patterns. They're a step above the fundamental building blocks of music and together form most of what you're writing. Some chords are easy to learn and play while other chords require a bit more background knowledge to understand and effort to master. But even without a proper musical education, if you've written enough music then you've probably used a few chords without even knowing it.
Did you steal it?
User avatar
requinix
Spammer :|
 
Posts: 5517
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What's with design patterns

Postby Live24x7 » Tue May 29, 2012 11:33 am

wow ..that makes it even clear :)
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: What's with design patterns

Postby kon » Tue May 29, 2012 2:24 pm

Design patterns are great for solving common problems and to have a common ground deciding a solution with others. Of course we shouldn’t learn those and think them as golden rules, almost each programming real life problem is not as simple and distinctive as the problems design patterns solves. More over if you have in your mind a premade solution there is the danger to modify the problem fitting your solution making the all design really mess. Don’t get me wrong, design patterns have their place in our knowledge, but after knowing as many of them, test and use them where each fit , you can consider them as your programming experience and not holly grails.
kon
Forum Newbie
 
Posts: 19
Joined: Sat Mar 03, 2012 6:43 am

Re: What's with design patterns

Postby Celauran » Tue May 29, 2012 2:29 pm

kon wrote:Design patterns are great for solving common problems and to have a common ground deciding a solution with others. Of course we shouldn’t learn those and think them as golden rules, almost each programming real life problem is not as simple and distinctive as the problems design patterns solves. More over if you have in your mind a premade solution there is the danger to modify the problem fitting your solution making the all design really mess. Don’t get me wrong, design patterns have their place in our knowledge, but after knowing as many of them, test and use them where each fit , you can consider them as your programming experience and not holly grails.

This is good advice. Beware the golden hammer.
PHP 5.6 released! August 28, 2014
PSA: PHP 5.3 end of life August 14, 2014.
User avatar
Celauran
Moderator
 
Posts: 3380
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: What's with design patterns

Postby Live24x7 » Mon Jun 04, 2012 2:16 am

@kon - thanks - very nicely explained.
That means - this is not something compulsory for a coder to know.
Infact knowing them may stifle creativity and the ability to search for original solutions.

@celaurun -
i had never heard of the term 'golden hammer'
read about it and couldn't have agreed more - if all you have is a hammer, everything looks like a nail
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: What's with design patterns

Postby Christopher » Mon Jun 04, 2012 3:56 pm

kon wrote:if you have in your mind a premade solution there is the danger to modify the problem fitting your solution making the all design really mess

I think this is really poor advice and perhaps a misunderstanding of patterns. They are not pre-made solutions. No wonder you worry about golden hammers if you think of them as such. The danger of implementing the completely common poor design is much greater than the hypothetical condition of patterns somehow harming the design.
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12662
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What's with design patterns

Postby kon » Mon Jun 04, 2012 4:55 pm

Christopher I would really like to learn your opinion about it, maybe I will learn something new (and I am not kiting). I agree with your first post and with Wikipedia ( http://en.wikipedia.org/wiki/Design_pattern ) . I am not advice anyone not to learn design patterns, the opposite. We should know them and use them a lot in experiment projects and learn their place in real life programming. For example I believe that we all have seen “Singleton” overused and not in right context. Why they do that? I believe simple because they had a “solution” , they modify the problem to fit the solution. So in that way it is a golden hammer (thank you celaurun for making me read more about the term). One more good link about design patterns is http://gee.cs.oswego.edu/dl/ca/ca/ca.html .
kon
Forum Newbie
 
Posts: 19
Joined: Sat Mar 03, 2012 6:43 am

Re: What's with design patterns

Postby Christopher » Mon Jun 04, 2012 7:03 pm

I don't think what you are describing is a golden hammer. What you are describing is the saying "when all you have is a hammer -- everything looks like a nail." You give a good example with the Singleton. Programmers usually start out by using a lot of global variables. It is the obvious solution, just not a good practice. So when they come to OOP they look for a design that will work just like their previous bad design. Hence the Singleton is misused. That is not the fault of the Singleton. The confusion is between thinking you want one instance of a variable (which is not the Singleton) and when you want only one instance of a class (which is a Singleton). The latter concept implies a solid understanding of classes that people new to OOP don't have. And if they did even a little reading about the Singleton pattern they would learn that it is not something you should use very often. There are warnings on most descriptions. They might even learn what an anti-pattern is!

I just think it doesn't make much sense to say, "People sometimes don't understand the best solutions to common problems, therefore they should keep using bad solutions." Design patterns are simply best practice solutions to design problems. They are misused because software design is difficult, not because patterns are somehow bad or a problem.

I would recommend that Live24x7 read about every pattern she/he can find. I recommend reading the GoF patterns first (search for them, here are a couple inks http://www.softpatterns.com/ or http://www.fluffycat.com/PHP-Design-Patterns/). Then would read Fowlers EAA patterns (http://martinfowler.com/eaaCatalog/). It takes years to understand when and how to apply them -- so you might as well start now. Design is very counter-intuitive so you need to slowly unlearn all the things you figured out on your own.
(#10850)
User avatar
Christopher
Site Administrator
 
Posts: 12662
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: What's with design patterns

Postby John Cartwright » Wed Jun 27, 2012 4:43 pm

Others have covered this topic well, so I won't regurgitate anything. Just want to say design patterns are not a concept universally you can apply to fix bad design. It is just as important to know when to use them as it is to know what they are.

As you learn more about them, you may even find you have solved problems with design patterns without even knowing it, because it made sense, and likely simplified your design.
Code: Select all
if ($toBe || $notToBe) echo 'That is the question'; 

NEW HERE?: Please read the Forum Rules, and take the Forum Tour before posting!
User avatar
John Cartwright
Site Admin
 
Posts: 11470
Joined: Tue Dec 23, 2003 3:10 am
Location: Toronto


Return to PHP - Theory and Design

Who is online

Users browsing this forum: No registered users and 1 guest