tag:blogger.com,1999:blog-36547168.post3386748286561841952..comments2023-11-25T11:00:42.257+01:00Comments on Invisible to the eye: Practical Php Patterns: Factory MethodGiorgiohttp://www.blogger.com/profile/03558287012747987157noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-36547168.post-55932805835253664592012-05-08T06:28:59.822+02:002012-05-08T06:28:59.822+02:00Nice presentation!I truly enjoy the way you explor...Nice presentation!I truly enjoy the way you explorer your experience and knowledge about the subject...Its been really helpful post for the developers as well as me.Cheers for this great sharing...Template Designhttp://www.webdesignbizz.comnoreply@blogger.comtag:blogger.com,1999:blog-36547168.post-87393980400530070932010-01-12T10:03:50.994+01:002010-01-12T10:03:50.994+01:00The article on Wikipedia has a Uml diagram on top ...The article on Wikipedia has a Uml diagram on top which corresponds to the classic pattern, but in the code samples it describes static creation methods. Since there is an interface for the creation methods, it makes no sense to make it static *in this pattern* (interfaces cannot contain static methods). In fact in the talk page:<br />http://en.wikipedia.org/wiki/Talk:Factory_method_pattern<br />there is a section "Article doesn't really describe the GoF pattern?"Giorgiohttps://www.blogger.com/profile/12689416577856305650noreply@blogger.comtag:blogger.com,1999:blog-36547168.post-67221683184022015172010-01-11T22:24:25.133+01:002010-01-11T22:24:25.133+01:00Hi Giorgio, what do you think of the examples foun...Hi Giorgio, what do you think of the examples found at wikipedia http://en.wikipedia.org/wiki/Factory_method_pattern#PHP <br />here they define an abstractProduct but abstractCreator and have different concrete creators with a factory method returning the concrete product. <br />Is this actually a good example of a factory method. I am often implementing them like this!<br /><br />Cheers, keep up the good writingAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-36547168.post-73141375233146368142010-01-11T17:52:28.213+01:002010-01-11T17:52:28.213+01:00Because during research I found out that people mi...Because during research I found out that people mixes the design pattern and the static factory method:<br />http://sourcemaking.com/design_patterns/factory_method<br />"An increasingly popular definition of factory method is: a static method of a class that returns an object of that class’ type. But unlike a constructor, the actual object it returns might be an instance of a subclass."<br />Note that the page is titled "Factory Method Design Pattern" and it's in the top 10 results of Google. It contains Uml diagrams for the classic GoF pattern.<br /><br />The immutable "newable" objects case is a nice example of a correct usage that I did not think of. :)<br />Though, I do not agree on the subclasses advantage, because I prefer not to refer to subclasses from a superclass (avoiding a mutual dependency between them).Giorgiohttps://www.blogger.com/profile/12689416577856305650noreply@blogger.comtag:blogger.com,1999:blog-36547168.post-77511342284277925612010-01-11T15:27:15.893+01:002010-01-11T15:27:15.893+01:00A static factory method is pretty much unrelated t...A static factory method is pretty much unrelated to the concept of the classic factory method pattern, so I'm not sure why you're mentioning it here as a misconception.<br /><br />Static factory methods are a direct alternative to the "new" operator that provide many advantages, like:<br /><br />1) Unlike constructors, they have names.<br /><br />2) They're not required to create a new object each time they're invoked. This is a huge advantage. Instance pooling, especially of immutable objects, can be extremely useful. See the Flyweight pattern which can be implemented this way.<br /><br />3) Unlike constructors, they can return an instance of a subtype. Thus you can return a different implementation without affecting clients.<br /><br />As far as testability is concerned, since it is in direct comparison to the "new" operator, there is no difference. "foo = new Foo(args)" is just as hard to mock as "foo = Foo.some(args)".<br /><br />I am certainly no fan of "static" but static factory methods are one of the rare cases where I still find them acceptable.Unknownhttps://www.blogger.com/profile/00439059243350222311noreply@blogger.com