SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    "Hello world" design patterns berserk

    Don't you just love design patterns? Uhhh...I had an insane idea and just had to implement it. It's "Hello world" in 160+ lines of PHP 5 code using several design patterns including MVC.

    It's longish, so I put it on a Web page:

    http://home.online.no/~reiersol/helloworld.html

    By the way, has this been done before? I haven't seen it anywhere.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eep!

  3. #3
    SitePoint Member
    Join Date
    Apr 2003
    Location
    Argentina
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, maybe it wasn't your original goal, but I've understood some concepts on Facade and Decorator with that example.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...I've understood some concepts on Facade and Decorator with that example.
    Brain is still digesting som of it Thanks btw for the examples - completely pointless as an application goes, but the example will shed a lot of light on the topic of design patterns for a lot of people

  5. #5
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Outstanding

    There's a definate need for some extra abstraction though.

    One problem I'm always running up against is new lines - "\n" is fine for the command line but for a web page you need "<br>" (or <br /> if it's XHTML!). Plus there's potential OS related issues to abstract out - Windows is "\r\n" for example.

    You could also get a command pattern into the controller. Might mean changing the use of the observer though.

  6. #6
    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 HarryF
    There's a definate need for some extra abstraction though.
    It's just a beta version. Or alpha. Or something like that. There's a shortage of letters at the beginning of the greek alphabet. There should be at least one before alpha and at least one between alpha and beta
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could also get a command pattern into the controller. Might mean changing the use of the observer though.
    This would be very interesting Yet to see an actual decent example of how the Command Pattern can be used from PHP.

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yet to see an actual decent example of how the Command Pattern can be used from PHP.
    Tell me about it. Recently blew it. But thats what its all about - discussion and working it out.

  9. #9
    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)
    In another thread I recently suggested that the way Phrame handles actions might be the Command pattern. Reconsidering, I think it is probably much more a strategy pattern.

    I think the point of the command pattern was much more GUI applicaiton oriented, e.g. undo support. I think given the stateless nature of a PHP application that it would be hard to implement the pattern as outlined by the GoF, or at least any implementation would be heavily dependant on the session?

    @HarryF and @dagfinn: Perhaps you guys could consider hosting this as an article over on phppatterns. I know you intended it as a spoof, but sometimes a really simple example is what it takes to begin to understand
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  10. #10
    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
    I think the point of the command pattern was much more GUI applicaiton oriented, e.g. undo support. I think given the stateless nature of a PHP application that it would be hard to implement the pattern as outlined by the GoF, or at least any implementation would be heavily dependant on the session?
    I'm not sure it's hard, but is it necessary? There are a few things you should be able to do with the command pattern, like undoing, redoing, logging and caching commands. I've never done any of those. On the other hand, the concept of a command or action--and especially identifying it in the HTTP request--is useful in Web apps. But the command pattern overshoots the target because having a separate class for each command is not necessary. In PHP, a simple function for each command will do the trick to start with, and you can extract functions or classes from those if it becomes too complex.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

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

    Quote Originally Posted by sweatje
    In another thread I recently suggested that the way Phrame handles actions might be the Command pattern.
    Martin Fowler considers the Struts actions to be a command pattern. See http://martinfowler.com/bliki/Comman...Interface.html

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

  12. #12
    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)
    Interesting. Especially:
    Quote Originally Posted by Martin Fowler
    if only because many people feel that a class needs a page or two of code to be worthwhile, and thus end up putting more code in the command than should be there.
    One thing that people have commented on in reviewing some of my applications is the number of Action classes with very few lines of code in the Perform method (and that is it). Oh well...sounds like I am at least doing that right

  13. #13
    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 lastcraft
    Martin Fowler considers the Struts actions to be a command pattern. See http://martinfowler.com/bliki/Comman...Interface.html

    yours, Marcus
    It's a class that handles an action, and an action is pretty synonymous with command. Having separate classes to handle each command (and a superclass, obviously) is enough to make it a case of the Command pattern, I would say.

    I don't know either Struts or Phrame well, but I wonder what they actually do with the Command pattern that's useful? What exactly does Struts do with Action objects that couldn't be done just as easily with a single method per action? With something as feature-rich as Struts, there probably is a reasonable answer, so I'm assuming it's a not as pointless as my "hello world" example. But knowing the answer would make us wiser. It would allow us to understand the so-called forces behind the Command pattern better.

    From DesignPatternsConsideredHarmful:

    I often use the VisitorPattern, not as a pattern, but as a red flag to simplify. Ever since I read the darned thing, I've wanted to use it somewhere. Quite often now, when I'm trying to resolve a complicated issue of communication between two objects, I find myself wondering whether I can apply the visitor pattern here. Usually, I take that as a cue that the communication is too complicated to begin with.

    So far, I've never actually implemented the VisitorPattern. The more times I don't implement it, the more valuable it is to me. --PhilGroce
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

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

    Quote Originally Posted by dagfinn
    I don't know either Struts or Phrame well, but I wonder what they actually do with the Command pattern that's useful?
    I know zilch about either, but I did build a FrontController system once and it did evolve into a command pattern driven framework. I started with just the strings, changed them to objects, found I could add error handlers and permissions checks to the objects and finally removed some ugly switching code by passing the core functionality through them. So it might just be because it is easier to implement (Jason?).

    Having the execute() method on tha action makes it a visitor doesn't it ?

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

  15. #15
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One of the primary ones is the ability to easily add common behavior to commands by decorating the
    Martin Fowler considers the Struts actions to be a command pattern. See http://martinfowler.com/bliki/Comma...dInterface.html

    yours, Marcus
    command executor. This is very handy for handling transactions, logging, and the like. Commands can be queued for later execution and (if the commands and their data are serializable) be passed across a network. Command results can be cached by holding results against a key synthesized from the command name and arguments.

    I thought the command pattern was more UI specific because it stores commands in a ArrayList allowing UNDO type functionality, but Fowler states it does... MORE!

    cheers

  16. #16
    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)
    Quote Originally Posted by lastcraft
    know zilch about either, but I did build a FrontController system once and it did evolve into a command pattern driven framework. I started with just the strings, changed them to objects, found I could add error handlers and permissions checks to the objects and finally removed some ugly switching code by passing the core functionality through them. So it might just be because it is easier to implement (Jason?).
    I think ease of maintenance is one of the driving factors of my use of Phrame. Whenever I revisit an application I have written, and say want to understand how a particuar form is being process. I know that I need to look at the form html to see what action is being specified as a hidden input, look at the map to see where that action maps to (which is usually not nescesary, becuase my naming convention is the action name followed by "Action" for the class name), and then look at that Action subclasses Perform() method.

    While it is easy to maintian, it is hard to do TDD becuase it is not loosly coupled. i.e. I tend to created instances of approprate model classes in the action rather than passing them in (becuase the passed parameters are defined by the framework, not per action).

    Quote Originally Posted by lastcraft
    Having the execute() method on tha action makes it a visitor doesn't it ?
    Might be a visitor if you wanted to do _every_ action's perform method, but the way Phrame is defined, you only execute one Action::Perform() per request. The reason I was leaning towards strategy is that the framework selected the correct action type, instanciates it, and calls the exact same method with same parameters. Seems like a strategy to me.

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

    Quote Originally Posted by sweatje
    While it is easy to maintian, it is hard to do TDD becuase it is not loosly coupled. i.e. I tend to created instances of approprate model classes in the action rather than passing them in (becuase the passed parameters are defined by the framework, not per action).
    What if the creation were moved to some kind of model factory in a registry? You could swap the whole factory out in the test setUp() for a mock one.

    Another might be to use a partial mock for the action if the creations were moved to separate protected methods. Difficult to test the routing table though. Does the map look for a class name?

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

  18. #18
    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 Resolution
    One of the primary ones is the ability to easily add common behavior to commands by decorating the
    command executor. This is very handy for handling transactions, logging, and the like.
    This is really a quote from Fowler, right? I remember having read it. If you had asked me two days ago, I would have said "Fowler is God". But this makes me wonder. if the object is to add common behavior, why not just add it to the code that calls the command? Like this:

    PHP Code:
    log($someArgs);
    editTopicCommand(); 
    Fowler is right that you can do useful things with the Command pattern, but I'm not sure adding common behavior is one of those things. Or did I miss something really important and obvious?
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

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

    Quote Originally Posted by dagfinn
    if the object is to add common behavior, why not just add it to the code that calls the command?
    I guess if you are writing a framework then this section of code should be off limits to app. developers. Perhaps this is the context he is describing.

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

  20. #20
    SitePoint Zealot
    Join Date
    Feb 2003
    Location
    Virginia
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dagfinn
    This is really a quote from Fowler, right? I remember having read it. If you had asked me two days ago, I would have said "Fowler is God". But this makes me wonder. if the object is to add common behavior, why not just add it to the code that calls the command? Like this:

    PHP Code:
    log($someArgs);
    editTopicCommand(); 
    Fowler is right that you can do useful things with the Command pattern, but I'm not sure adding common behavior is one of those things. Or did I miss something really important and obvious?
    I was thinking about this this week in regards to PULL style programming - ala WACT.

    Command could be used to que ALL commands without EXECUTING THEM and then if there where no errors or emergency exits then the ArrayList is executed. I'm thinking of it in terms of the ultimate lazy loader.

    Would also make for great debug too.

    Res

  21. #21
    SitePoint Zealot NinjaNoodles's Avatar
    Join Date
    Oct 2006
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excuse me for asking, but what are design patterns?

  22. #22
    Put your best practices away. The New Guy's Avatar
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    2,087
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by NinjaNoodles View Post
    Excuse me for asking, but what are design patterns?
    http://en.wikipedia.org/wiki/Design_patterns
    "A nerd who gets contacts
    and a trendy hair cut is still a nerd"

    - Stephen Colbert on Apple Users


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
  •