The Singleton Chapter
I was reviewing the editing of the Singleton chapter last night. It was kind of interesting to look it over again, as I originally wrote, and had the tech review performed, on this chapter back in December of 2004! Looking it over again I am pretty proud of the decisions I made in writing it. One important decision was to not make the Singleton pattern the first design pattern explained in the book. The actual sequence of chapters up to this point is:
- Programming Practices
- The ValueObject Pattern
- The Factory Pattern
- The Singleton Pattern
So the Singleton pattern is the third pattern covered in the book. The Programming Practices chapter gave me a chance to introduce Unit Testing and Refactoring, which is very important considering the heavy use of SimpleTest throughout the rest of the book for unit tests of the code. As I mentioned in an earlier blog entry, the unit tests are not just included in the code download, but integrated directly into the prose and flow of each chapter. The ValueObject pattern discussion gave me a chance to review references in PHP4 and object handles in PHP5 in much greater detail, and doing FactoryMethod next lets me allude to the fact that the Singleton is essentially a specialized Factory which only returns a single instance of a class.
In the book I try to give equal coverage to PHP4 and PHP5, but this particular chapter I spend much more time with PHP4 examples, as having private constructors in PHP5 make implementing the pattern relatively easy. I cover several possible implementations, and highlight the fact that the Zend 1 engine does not store references in static variables (as noted in the manual).
I briefly cover the MonoState pattern here as well, since they end up exhibiting similar behaviors, and you can do some neat code with reference —non-object related references!—to implement the MonoState pattern.