O'Reilly logo

Core PHP Programming, Third Edition by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

29.3. Factory

Factory is a design pattern aimed at decoupling the instantiation of your objects from the application code that uses them. For example, you may want to use different kinds of objects depending on the situation. If you have two rendering classes, HtmlRenderer and WmlRenderer, and want your application to transparently use the right one depending on what kind of client is connected, you can easily do that using the Factory design pattern.

There are many different variants of the Factory design pattern. In Figure 29.2 we pick a simple one, which simply uses a global function.

Figure 29.2. Factory pattern.
 <?php //define abstract factory class class Renderer { private $document; abstract function render() { } function setDocument($document) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required