As you'll certainly recall from the prior section, my intention here is to demonstrate how a static method can be really useful in certain cases, particularly when it's necessary to spawn a determined number of objects, but without having to create an instance of the factory class that builds the objects in question. This concept is perfectly applicable to all of the DIV classes that were defined previously, so let me show you a comparative example where two different factory classes are used to instantiate a few DIV objects, first by using a conventional factoring method, and then by using a static method. Having said that, here is the signature of the first factory class, which in this case defines a conventional factoring method for spawning some DIV objects: // define 'DivFactory' class - no static method is used in this public function createDiv As you can see, the previous class has only one factory method, called "createDiv()", which is responsible for returning to client code a specific type of DIV object. Obviously, the method is callable from inside the object context, which implies that it can be used easily to instantiate some basic DIV objects, as indicated below: try{ // use 'DivFactory' class to spawn some div objects - no static // display DIV elements on the browser If you study the previous code sample, then you'll realize that its implementation is rather inefficient, since it creates an unnecessary instance of the factory class to spawn three different DIV objects, which are finally used to display the real containers on the browser via their "display()" method. Quite possibly, at this very moment you're wondering what's wrong with creating an instance of the factory class. Actually, not much, but this approach can be improved if a static factory method is used instead of a conventional one. To help you understand this concept, below I redefined the previous factory class in such a way that it now uses a static method to create instances of the three DIV objects that were shown in the prior section. The signature of this improved factory class is as follows: // define 'DivFactory' class - static method is used public static function createDiv As shown above, the factory class looks nearly the same as the one in all the previous examples. Of course, the only difference here is the declaration of its factoring method static. This means that it can be perfectly callable from outside the object context, thus avoiding the unnecessary instantiation of the pertinent factory class. Pretty neat, right? To reaffirm this concept, below I listed an example that shows how to spawn three different DIV objects, without using any instance of the factory class in question: // use 'DivFactory' class to spawn some div objects - factory // display divs elements on the browser See how useful it is to utilize a static method to create the previous DIV objects? I guess you do! Please, notice how the aforementioned DIVs are properly spawned by calling statically the corresponding "createDiv()" method. In this case, hopefully I provided you with a concrete example where a static method is used in a helpful fashion for implementing the popular factory pattern. All right, at this stage I'm sure that you already have an accurate idea of how to take advantage of the remarkable functionality provided by static methods in PHP 5. In the next section of this tutorial I'll be listing the source code of all the classes built here, so you can use them to develop your own testing examples. Click on the link below and keep reading, please.
blog comments powered by Disqus |
|
|
|
|
|
|
|