A good place to start turning the form element factory class into a Singleton is by taking a look at its definition for use in PHP5. As you hopefully remember, the class looks like this: abstract class formElementFactory{ As I mentioned before, the class is defined as abstract, and presents a single functional method, “createElement()”, which carries out the instantiation of form objects, as well as the implementation of Polymorphism. It does this through the “getHTML()” method, which is useful for returning the proper (X)HTML code for each form element. A possible use of the class would be the following, assuming that form element classes have already been defined: // make array with parameters to be passed to the class $formElements=array('textinput'=>array('name'=>'username','value'=>'', foreach($formElements as $element=>$attributes){ // display form elements echo formElementFactory::createElement($element,$attributes).'<br />'; } Through the above lines of code, we first created a few form objects, and then displayed their HTML code, by using a “foreach” loop. Although basic, the example reveals the strengths of the factory class, since rendering form elements becomes an extremely easy process. As you can see, working with an abstract class in PHP5 is straightforward. However, when programming in PHP 4, things are a bit more messy. This is particularly true if we’re working with a form factory class, while a single instance of it is needed. Using references may certainly help, due to the fact that we’re not spawning multiple copies of an object. When building large applications, however, tracking the existence of an object can sometimes be a cumbersome task. There’s a better approach to solving this issue. By applying the Singleton pattern, we can make sure that only a single instance of the factory class is available across the program, and other objects are using this unique instance. Based on this concept, making the class a Singleton is as simple as this: class formElementFactory{ Let’s analyze the Singleton version of the class. Notice the existence of the same “createElement()” method that returns a new form object, according to the type of input passed to it, which is identical to the PHP5 incarnation. Until now, we've seen nothing unexpected. However, there’s another relevant method, “getInstance()” that makes sure that only a single instance of the object is always returned. By declaring a static $instance variable, we’re ensuring that once an instance of the object has been created, no additional instances will exist. Just in case you’re not aware of this: when a static variable is defined as static within a function scope, it won’t lose its value, even if the program has left this scope. Therefore, because of the static type of the $instance variable, the following expression: if(!isset($instance)){ always returns a single instance of the object. As a result, a Singleton version of the factory class has been defined. Considering this, whenever a PHP program creates an object belonging to the class, a unique instance will be available within its execution. Perhaps the implementation of the Singleton pattern is a little harder to understand. Definitely, the concept will be understood best by taking a look at some examples. Join me in the next explanation to see how a Singleton class can work along with some form element classes.
blog comments powered by Disqus |
|
|
|
|
|
|
|