HomePHP Decoupling the Interaction Between Objects with Factory Methods
Decoupling the Interaction Between Objects with Factory Methods
Welcome to the fifth part of a six-part series on implementing factory methods in PHP 5. In this part, I show how the removal of a factory method inside the constructor of the class that originates persistent objects can produce a huge impact in the way that those objects handle its dependency, which happens to be a database handler object.
Unquestionably, defining factory methods during the development of an application is a task that both programmers and web developers must tackle frequently. In the case of PHP 5, this process is fortunately a no-brainer one, due to the versatility of its robust object model and its flat learning curve.
While the correct implementation of factory methods in PHP 5 is certainly a tame procedure for developers who have solid experience, it can be quite challenging and intimidating for those who are currently taking their first steps in object-oriented web development.
So, if you’re interested in learning the key concepts that surround the definition of factory methods in PHP 5, then start reading this article right away!
And now that you've been introduced to the subject of this series, it’s tine to review the topics that were discussed in the last article. In that installment of the series, I demonstrated how useful a factory method can be for reducing the number of instances of a database handler that need to be used by other classes.
Basically, this factory method returned a Singleton instance of this database handler, which was shared seamlessly by a couple of persistent objects, thus improving the way that these objects managed their interaction with this dependency.
However, in its current state, the class that originates those persistent objects is still responsible for creating the database handler, which is detrimental in terms of building truly decoupled classes. Thus, in this fifth part of the series I’m going to modify the signature of the class that models persistent objects, which will contribute to making them more independent from the aforementioned database handling object.
Now, it’s time to leave the preliminaries behind and learn more useful things about building factory methods in PHP 5. Let’s go!