HomePHP The Singleton and Factory Patterns in PHP: a rendering-capable factory class
The Singleton and Factory Patterns in PHP: a rendering-capable factory class
In this third part of the series, I will explain how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects. This will boost the functionality of the factory class by simplifying the process for rendering form elements.
Welcome to the third part of the series “The Singleton and Factory Patterns in PHP.” As you probably remember, in the second part of this series, I applied the functionality of the Factory pattern to taking the process of regular form generation from an object-oriented point of view, branching to a different direction from the traditional way for coding web forms.
Essentially, the form generation process can be divided into three key categories: element class definition, form element factory class definition, and finally visual presentation. As you might guess, the first one refers to the stage at which all of the form elements are structured as individual classes, the second implies specifically the definition of the required form factory itself, while the third stage is tasked with rendering each form element according to the specific needs within the program.
So far, I’ve demonstrated a basic implementation of the abstract “formElementFactory” class, which is responsible for creating form element objects, by separating object instantiation from the rest of the application code.
As I mentioned previously in the second part of the series, there are several features that must be added to the logic of the form application, in order to get a more functional version of it, by satisfying several requirements often present in real environments.
Certainly, the first issue to be properly addressed is the rather ineffective implementation of Polymorphism for rendering form elements. The second issue is the lack of control in the overall presentational layout of the form.
For most of these problems to be efficiently solved, we need to apply some refactoring to the form element factory. We do this by adding the capability to take advantage of the polymorphic features exposed by each form element object, as well as implementing a basic control on the visual presentation for every element.
In this part of the series, I shall provide an overview of the solutions for tackling the above conflicting topics, while maintaining a strong practical approach in the use of design patterns.
So, it’s time to learn more about applying design patterns to object-oriented forms. Let’s get started.