News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Ossigeno Blog:
Factory for everything
May 28, 2009 @ 11:13:20

On the Ossigeno blog there's a recent post from giorgio that suggests that you should use a factory design pattern as often as you possibly can.

Why using a factory to produce simple objects? Because new keyword has to be isolated, even if it is only new Form(); [...] The descripted design is classic test-driven development: ask for dependencies in the constructor, so you can test the class in isolation by passing in mocks objects; then build factories that have methods for creating the top-level objects that you use in your script (or controllers, or views).

He includes a few code examples of what a factory pattern looks like and gives his reasoning as to why this should be a best practice:

Because the point is in abstracting away the process, much like the Law of Demeter suggest to do with composed/aggregated object methods. If the constructor is going to change, just one line of code has to be edited to accommodate new dependencies.
0 comments voice your opinion now!
opinion designpattern factory


blog comments powered by Disqus

Similar Posts

Padraic Brady's Blog: Namespaces (or Yet Another Pointless Opinion Piece)

DevShed: Handling Cookies and File Data with the Factory Pattern in PHP 5

Jani Hartikainen's Blog: You donít need a service layer: Fat controllers are okay

Nikita Popov's Blog: Don't be STUPID: GRASP SOLID!

DotNetButchering: Design Patterns you use without knowing them


Community Events





Don't see your event here?
Let us know!


language install security symfony community opinion library voicesoftheelephpant package laravel podcast release interview tool composer application version framework series introduction

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework