Indeed, I can go one step further by polishing the form’s visual presentation, and define a sample class “Page”, which builds the basic structure of a web page. Doing so, we’re able to combine the functionality of this class along with “formElementFactory”, for a quick and dirty implementation. So, the definition for the simple “Page” class is the following: class Page { In simple terms, the above class presents the classic methods for building the header, body and footer sections of a web page, while its constructor sets up a default sample page. Definitely, I won’t stop explaining its logic. Instead, I’ll show an example that uses this class to generate a login form. Here is the sample code: // instantiate a new Page object // build header page section // add form tag and build a table header // build a text input box // build a password field // build a submit button // add the code to the body section // build the footer page section // display the page The above code uses the “Page” class to generate the structure of a web page, by displaying the header, body, and footer parts. Notice the rough usage of the “formElementFactory” class to obtain the code for the required form elements, by decoupling object instantiation from the general form rendering process. The only point worth mentioning on the above example is the extensive use of the static “createElement()” method, which is naturally invoked without the need to explicitly instantiate an object from the form element factory class. As you remember, this class has been declared as abstract, therefore any attempt to instantiate an object from it will result in the PHP interpreter triggering a fatal error. Despite the fact that I’ve hidden behind the structure of the factory class all of the required processing for displaying form elements, I’m still far away from having an efficient rendering mechanism. Notice that the rough use of the “Page” class is merely an example, not suitable for use in a fully functional application. However, the factory class works quite well, by implementing the Factory pattern, for solving annoying tasks involved in displaying programmatically web form elements. Now, I’ve exposed a common problem that comes up when building real-world applications that can be addressed in an effective way, in this case by applying a design pattern. Wrapping up That’s all about it for now. In this third part of the series, I’ve explained how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects to boost the functionality of the factory class, by simplifying the process for rendering form elements. Now, let’s pay attention to the next part of the series, where I’ll explain the correct application of the Singleton pattern, in order to work with only a single instance of a form factory class. As you’ll see in short, making a class a Singleton is quite easy to do, allowing you to write robust PHP4 programs, by reducing the hassles related to manipulating multiple object instances.
blog comments powered by Disqus |
|
|
|
|
|
|
|