Well lately I've come across some interesting PHP code in wikipedia's design pattern sections such as builder and visitor(not for all though). The code looks like this:

http://en.wikipedia.org/wiki/Builder_pattern
http://en.wikipedia.org/wiki/Bridge_pattern
http://en.wikipedia.org/wiki/Visitor_pattern

Look like some kinda of parody of Java programming if you ask me. I was wondering though, are there people who actually code PHP in this style? I mean, everything is enclosed in class except for the very one line calling Class::main()?

    Looking at the example for the visitor pattern, my guess is that someone decided to take the Java example and directly translate it into PHP, forgetting that the Tester class and its main method is unnecessary, heheh.

      laserlight;11013297 wrote:

      Looking at the example for the visitor pattern, my guess is that someone decided to take the Java example and directly translate it into PHP, forgetting that the Tester class and its main method is unnecessary, heheh.

      Yeah, but to some extent PHP5 does resemble a lot from Java, the syntax of static, final, abstract, and interface are pretty much the same as Java's. PHP's perhaps the second most similar language to Java right behind C#, whether it's a good or bad thing is up for discussion though. XD

        I really don't exactly know what will be the benefits of it it just a "Thinking style", I remember that I done the same idea of builder when I made a class of the side block in one of my projects.

        I don't see any innovative here, Most of us made the builder pattern even without reading about it.

          2CODE;11013377 wrote:

          I really don't exactly know what will be the benefits of it it just a "Thinking style", I remember that I done the same idea of builder when I made a class of the side block in one of my projects.

          I don't see any innovative here, Most of us made the builder pattern even without reading about it.

          Well Builder pattern can be quite useful in some cases, good examples are HTML Table and Form classes. Both table and form can have various elements and operations and are easily getting huge. A table builder and form builder class can help produce more organized and logical code. And I was wondering though, are Java's StringBuilder and StringBuffer using builder pattern?

            2CODE;11013377 wrote:

            I really don't exactly know what will be the benefits of it it just a "Thinking style", I remember that I done the same idea of builder when I made a class of the side block in one of my projects.

            I don't see any innovative here, Most of us made the builder pattern even without reading about it.

            Well...isn't that essentially the definition of a software design pattern: a [relatively] common approach to solving a [relatively] common problem? (Granted, the ones I'm most interested in are those solutions I haven't thought of yet myself, but I suppose there will always be a gray area between what's obvious to me and is not to someone else.)

              Well I was wondering if this is considered a good example of using builder pattern. Let's say you have a user private message system. You can create a class called PrivateMessage which uses data from your database table prefix_privatemessages. You are happy and everything is fine, but soon you realize that the system is not working well for new private messages being composed. New private messages do not have data stored in your database yet, and it will be a pain to accommodate for them by editing the constructor and subsequent methods to deal with them. A better idea may be to create a private message builder class, which is specialized at creating new private messages, validating them before inserting into database, and finally producing a concrete private message object if asked. Of course we can also create a different class called NewPrivateMessage which handles private messages not yet inserted into database, but I wonder whether it will be better and more efficient than PrivateMessageBuilder?

                Lord Yggdrasill;11013369 wrote:

                Yeah, but to some extent PHP5 does resemble a lot from Java, the syntax of static, final, abstract, and interface are pretty much the same as Java's. PHP's perhaps the second most similar language to Java right behind C#, whether it's a good or bad thing is up for discussion though. XD

                That's because PHP's object model was cribbed off of Java, which in turn was solidly conventional when it came to its choice of object model.

                  2CODE wrote:

                  I don't see any innovative here, Most of us made the builder pattern even without reading about it.

                  That is part of the idea of design patterns: they are supposed to be recurring patterns of design that are given a (hopefully) descriptive name with the form of the pattern laid out to ease identification and hence recall, discussion and reuse.

                    Write a Reply...