SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Most useful design patterns for web applications?

    I am going to present a talk on design patterns in PHP at php|tek in Chicago this May. In the past I have presented a catalog of design patterns with reference examples in PHP. The selection was based on my experience in using these patterns myself, as well as collective knowledge from the web (like reading this forum ).

    I am interested in doing a serious update to the talk with two main objectives:
    1) narrow the focus to fewer patterns
    2) show some examples of "seen in the wild" as well as relatively simplistic reference examples

    With those goals in mind, I would like to solicit you opinion on these two subjects. Relative to point 1, what are the design patterns you have found most useful in your coding? Don't feel you need to limit yourself to one, if you have several favorites, let me know. Why are these patterns useful to you?

    Second, have you seen some good example of design patterns in any open source projects you have worked on? Feel free to stump your own project, or some other project's code you have reviewed, but let me know where you see a pretty clean example of a common design pattern.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  2. #2
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't get around MVC if you count that as a separate pattern. Then I would say the two I use most are Front Controller and Data Mapper. Of course, frameworks tend to give you a ready-made implementation of Front Controller and some kind of data access pattern.

    Composite View is usually necessary in some form, although I think it would have been more useful to think of Composite View as a problem that can solved with different patterns, rather than as a pattern in itself.

    Then there's Intercepting Filter or Interceptor as it seems to be called nowadays. It's pretty necessary for clean implementation of authentication.

    And it could be used more. In Struts 2, they seem to have moved most of the Front Controller functionality into Interceptors. I like that lot. (That's a Microkernel pattern, in which the Front Controller is left with a minimum of functionality.)

    I should also mention View Helper, although in this case, too, I'm queasy about the fact that it's defined as one pattern.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I was debating putting something into my original post about MVC. I almost think of it as a meta-pattern, or a architecture guideline more than a pattern now. Is there an open source project you believe has a representative example of a Data Mapper? Seems like most frameworks shoot for an Active Record or Table Data Gateway instead.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Addict
    Join Date
    Jan 2008
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use a mix of the following

    MVC
    Intercepting Filters
    Composite Views


  5. #5
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje View Post
    I was debating putting something into my original post about MVC. I almost think of it as a meta-pattern, or a architecture guideline more than a pattern now.
    That's pretty much exactly how I think about it, too.
    Quote Originally Posted by sweatje View Post
    Is there an open source project you believe has a representative example of a Data Mapper? Seems like most frameworks shoot for an Active Record or Table Data Gateway instead.
    Not really. I googled and found a project called PHPDataMapper, except it's hardly implemented yet:

    http://phpdatamapper.com/

    I also found a project that claims to be a Data Mapper implementation, but isn't. Possibly, some believe that anything that does object-relational mapping can be called a Data Mapper.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  6. #6
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @dagfinn - Thanks for the mention.

    Quote Originally Posted by sweatje View Post
    Is there an open source project you believe has a representative example of a Data Mapper? Seems like most frameworks shoot for an Active Record or Table Data Gateway instead.
    That's the exact reason why I made phpDataMapper. Most model and ORM layers that claim to be data mappers have things like $row->save(), which goes against the very definition of a data mapper. The phpDataMapper goals page has some more of the core philosophies explained. The project itself is still in early beta, which is why I have not packaged it up into a downloadable archive file yet. It's open-source MIT licensed and you can get the code via SVN right now if you want to take a look.

    The main things that remain to be done on it before the first "stable" release are:
    1. Table structure migrations
    2. Cascading deletes for table relations (For RDBMS that do not support them natively)
    3. Pagination
    4. Additional cleanup, reorganization, and testing


    So although it's not in an officially "stable" state yet, I am using it in production on InvoiceMore with no issues, mostly because since I built it, I know what works well and what doesn't. Let me know if you would like to know more, or if you plan to include it in your talk. I've made some additional updates to the project for InvoiceMore that I haven't checked in yet along with the other issues listed above that I plan to solve very soon (before your talk in May).

  7. #7
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @Czaries I will keep an eye on it.

    Now thinking about restructuring the talk around MVC as architecture guideline, so talk about data persistence related patterns in model section, view and controller related patterns in their respective sections.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  8. #8
    SitePoint Addict webaddictz's Avatar
    Join Date
    Feb 2006
    Location
    Netherlands
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje View Post
    With those goals in mind, I would like to solicit you opinion on these two subjects. Relative to point 1, what are the design patterns you have found most useful in your coding? Don't feel you need to limit yourself to one, if you have several favorites, let me know. Why are these patterns useful to you?

    Second, have you seen some good example of design patterns in any open source projects you have worked on? Feel free to stump your own project, or some other project's code you have reviewed, but let me know where you see a pretty clean example of a common design pattern.
    That'll sure be a nice talk How about the Registry pattern, which you can then perhaps compare to the Service Locator pattern. I also really like the Strategy pattern. Perhaps you might even want to Dependency Injection, although I think it's hard explaining in a short talk.
    Yes, I blog, too.

  9. #9
    SitePoint Addict
    Join Date
    Nov 2005
    Location
    Germany
    Posts
    235
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Particularly useful: Template View, Registry, Lazy Loading and the simple Factory (Method)--I'm not sure the last two count as patterns.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree that MVC is a meta-pattern, but still very valuable nonetheless. It's definitely been a turning point for me. I'm a big fan of Front Controller and Singleton, too. I don't often use the Registry pattern, but I feel it's a very good one to know since it eliminates (or helps to) polluting the global namespace.

    edit: I forgot about Active Record Database. That plus MVC are the top 2 for me.

  11. #11
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Patterns relating to service locating that when implemented fade entirely into the background: things get located for you and Just Work. Ways to do things such as action and view helpers in Zend Framework, on-demand and unobtrusively – not saying the way things are done in Zend are optimal or close to it, but they've got the general direction right.

    Oft-used auxiliaries to MVC such as intercepting filters, routing and composite views, as mentioned, are well worth an introduction too.

  12. #12
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ezku View Post
    Patterns relating to service locating that when implemented fade entirely into the background: things get located for you and Just Work. Ways to do things such as action and view helpers in Zend Framework, on-demand and unobtrusively – not saying the way things are done in Zend are optimal or close to it, but they've got the general direction right.
    I would say that action helpers are an unnecessarily cumbersome way to make an object available to the controller. Perhaps they're easy to use once they're implemented, but implementing them is basically unnecessary. At least in the examples I've seen, they appear to be just superfluous middle men that could be avoided fairly easily if dependency injection were available.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  13. #13
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi...

    I suspect I am no longer the right person to answer this .

    Dependency injection has wiped out a vast sway of architectural and construction patterns for me. DI even rejuvinates the humble PageController.

    Most of my data mapping patterns have been replaced. I'd reach for an off the shelf ORM for persistent stuff close to the UI. This is simple enough for an ORM and the changes tend to be driven by the code, making it a good idea to write the data stuff in code form.

    For more data centric apps, those that need a bit of data modeling, I prefer this:

    PHP Code:
    $upgrade $accounts->upgradeToYearly($purchase
    This gets trivially translated into...

    Code:
    select upgrade_to_yearly(purchase);
    I.e. a stored procedure. I find things like accounting operations are better handled in SQL. The business rules for account management exist outside the application, sufficiently decoupled that changing language is no problem at all.

    This doesn't seem to have a pattern name. Perhaps the BailOutIntoTheMoreAppropriateParadigm pattern?

    I'm finding this a hard question to answer, because so much design pattern stuff is internalised to me now. Five years I could think and classify in patterns, and I was trying to popularise things like MockObject and DependencyInjection. Nowadays I try to write the code in my ideal DSL and tend to hide patterns in my code.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  14. #14
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you could cover a lot of ground with just Factory, Dependency Injection, Template Method, and Strategy.

  15. #15
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to checkout dORM's codebase (getdorm.com). It implements most ORM patterns: Unit Of Work, Lazy Load, Identity Map, Data Mapper and Metadata Mapping. If you have any question, feel free to hit me up on IRC (freenode.net/#dorm).

    Good luck with your talk !


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •