News Feed
Sections




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

Allan MacGregor:
Design Patterns in PHP Using Factories
January 21, 2014 @ 09:39:32

Allan MacGregor has a recent post to his site looking at another design pattern (previously he discussed dependency injection), this time it's the use of factories.

The factory pattern is one of the most commonly used Design Patterns, it is useful when we need to choose between several interchangeable classes at runtime. By using factories we are also separating the creation of an object from the actual implementation. We can define a factory as a simple design pattern that give us a convenient way to instantiate objects. A factory is capable of creating different types of objects without necessarily knowing what type of object is actually being created.

He talks some about the problems that factories try to solve and some of the common implementations - simple, abstract and the factory method. He also covers some times when it's useful to use the pattern (including the creation of complex objects). The rest of the post includes sample code showing the creation of a Product class, creating them manually and the refactor over to a factory created method, making it more flexible for products in the future.

0 comments voice your opinion now!
design pattern factory introduction refactor product

Link: http://coderoncode.com/2014/01/19/design-patterns-php-factories.html

blog comments powered by Disqus

Similar Posts

Master Zend Framework: Configuring the ServiceManager with Abstract Factories

DevShed: Iterators in the Simplest Sense - An Accessible Implementation in PHP 4

php|architect: A developerís introduction to Drupal (webcast recording)

Lorna Mitchell: How NOT to Design Your API

Matthias Noback: Inject a repository instead of an entity manager


Community Events





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


framework configure code podcast library release series community application wordpress project api interview bugfix laravel introduction developer threedevsandamaybe list language

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