In the introduction, I mentioned that the factory class created in the preceding tutorial had a pretty annoying down side, since it was necessary to first create an instance of it to invoke its factory method. Below I’ve listed for you the source code of this class, along with an example that shows how to use it for creating some web form element objects. First, here are the corresponding classes that render HTML input boxes and text areas. Take a look at them: // define TextBox class class TextBox { private $name = 'mytextbox'; private $id = 'textbox'; private $class = 'textbox'; private $maxlength = 20;
// constructor public function __construct($attributes = array()) { if (empty($attributes) === FALSE) { foreach ($attributes as $attribute => $value) { if (in_array($attribute, array('name', 'id', 'class', 'maxlength')) and empty($value) === FALSE) { $this->$attribute = $value; } } } }
// renders input text box element public function render() { return '<input type="text" name="' . $this->name . '" id="' . $this->id . '" class="' . $this->class . '" maxlength="' . $this->maxlength . '" />'; } }
// define TextArea class class TextArea { private $name = 'mytextarea'; private $id = 'textarea'; private $class = 'textarea'; private $rows = 10; private $cols = 20;
// constructor public function __construct($attributes = array()) { if (empty($attributes) === FALSE) { foreach ($attributes as $attribute => $value) { if (in_array($attribute, array('name', 'id', 'class', 'rows', 'cols')) and empty($value) === FALSE) { $this->$attribute = $value; } } } }
// renders textarea element public function render() { return '<textarea name="' . $this->name . '" id="' . $this->id . '" class="' . $this->class . '" rows="' . $this->rows . '" cols="' . $this->cols . '"></textarea>'; } } As seen above, the logic implemented by the “TextBox” and “TextArea” classes is quite simple to grasp; in both cases, they accept an array of attributes, which are used by their “render()” method to display the corresponding HTML elements of a web form. The workings of those classes are that simple, really. Now, it’s time to show the definition of the factory class, which as I explained before, is responsible for creating both input text and text area objects for further manipulation. Here’s how this class was conceived originally: // define FormElementFactory class class FormElementFactory { public function factory($formElement, $attributes = array()) { if ($formElement === 'textbox' or $formElement === 'textarea') { return new $formElement($attributes); } } } Obviously, the above class is a wrapper for its factory method, which is the actual workhorse, since it’s tasked with returning to client code the aforementioned web form element objects. As I pointed out earlier, though, while the factory method does its thing as expected, it’s necessary (at least in theory) to create an instance of its originating class to use it. The following script clearly represents this situation: $formFactory = new FormElementFactory(); // create some form elements objects $texBox = $formFactory->factory('textbox'); $textArea = $formFactory->factory('textarea'); // display form elements echo $texBox->render() . '<br />' . $textArea->render(); Unquestionably, the form element factory does a decent job when it comes to creating input box and text area objects, but as seen above, this process requires spawning an instance of it first, which is pretty inefficient. Of course, it’s valid to say that the class’s factory method could have been called out of the object scope, that is statically, but this would mean getting ahead of myself. For the moment, this factory method hasn’t been declared static, so it’s necessary to find a partial solution that permits us to create only a single instance of the previous factory class. But how can this be achieved? Well, I used the term “single” on purpose, so you can pick up the solution very quickly! Yes, in this case I’m talking about adding to the factory class a method that returns Singletons of it, thus letting us avoid dealing with multiple instances of the class. Sounds pretty interesting, right? Thus, in the next section I’m going to show you how to incorporate this brand new method inside the previous factory class. Now, click on the link below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|