Factory Method Design Pattern

Factory Method Design Pattern

The factory method design pattern (see Design Patterns, by Gamma, Helm, Johnson, and Vlissides, Addison-Wesley, 1995) is a creational design pattern, which means that it is characteristically charged with the creation (instantiation) of objects. But why might you want to complicate your life by using a design pattern, rather than simply using new, asin $variable = new MyClass()? There are two main scenarios where it might prove useful:

  • Runtime flexibility: Sometimes it's impossible to choose ahead of time which specific object should be instantiated. The choice of objects to use may depend on something in the runtime environment such as user input, environmental settings (for example, internationalization), or even hardware/software support.

  • Abstraction: By using a factory interface, you can delegate the instantiation of your application's classes to subclasses. This means your entire application can be constructed on the assumption that a set of generic, abstracted classes will be used as a proxy for real utility classes. This allows you to develop with a view to integrating only with those abstracted classes, rather than directly with the useful classes they represent.



Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 182

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net