In order to start demonstrating how the builder pattern can be used for constructing online forms, first I need to define a target object that can be "manipulated" by the corresponding director and builder objects. First I'll define an abstract "FormElement" class and then I'll derive a child from it, which will offer a concrete implementation for all its abstract methods. That said, here is the initial definition of the "AbstractFormElement" class:
If you were thinking that creating an abstract form element class with PHP was a hard-to-perform process, then I believe you'll find that you're wrong. As usually, the previous class defines the generic structure of a typical form control, and also presents an abstract method called "getHTML()," which will be responsible for returning to calling code the appropriate (X)HTML markup that corresponds to the online form in question. As you can see, this is nothing unexpected. Now, let me take one step forward and create a child class from the "AbstractFormElement" class defined earlier. The signature of this new sub class is listed below, thus examine its source code:
As illustrated above, now the new child "FormElement" class offers a concrete implementation for each of the methods declared in the base class. Of course, the most important thing to note here concerns all the tasks performed inside the constructor. As you can appreciate, this method really does all the hard work required for creating the (X)HTML markup that corresponds to each element of a regular online form, based on the pair of arrays supplied as input arguments. Finally, the respective form's code is retrieved by the tight "getHTML()" method. Simple and powerful! Well, now that you know how the target object (in this case referenced by the "FormElement" class) has been correctly defined, the next step consists of creating the corresponding builder class, in this way introducing the second component required by the builder pattern (remember that the third one is the director). In the next section I'll be defining the mentioned builder class, therefore I recommend you click on the below link and keep on reading. See you there.
blog comments powered by Disqus |