Eeeeek! A statistic! Relax, it’s just a Chi Square. The secret to understanding complexity is decomposition and modularity. Decomposition refers to breaking down a whole into its elements. Ironically, the term is fundamental to parallel and concurrent programming where a basic process in programming, like a loop, is further decomposed into more than a single loop, but decomposition also applies to standard OOP programming as well. Any complex process broken down involves the decomposition process. Once broken down, you can attend to the individual modules—modularity.

The Template Method design pattern is a great way to modularize a complex problem, like working through a Chi Square statistic. Chapter 9 in Learning PHP Design Patterns, explains the Template Method in detail and all about “The Hollywood Principle,” but here, I want to look at the Template Method as a “modulaizer”—making complex problems manageable. To get started take a look at Figure 1:

Figure 1: 2 x 2 table with row and column totals

Imagine that the data in the table are based on a survey you did at a PHP group, like the Boston group. Suppose that you want to find out the members language preferences based on whether the member owns a Macintosh or a Windows PC. (Sorry Linux.) You’re interested in PHP and jQuery and you record the responses in a two dimensional array. You find that the PC users favor PHP and the Mac users are evenly split between PHP and jQuery. Getting the table results in a 2 x 2 matrix like the one in Figure 1, you’re interested in whether there’s any real significance in the differences or the whole thing is random. You decide to use a Chi Square statistic to find out. Before going further you can run the application and download the files using the buttons below:

It Started with a Multi-Dimentional Array

This post came out of a Boston PHP group Percolate session for advanced PHP and OOP programming. Working through Larry Ullman’s PHP Advanced and Object-Oriented Programming, 3rd Ed book, the first chapter covered multi-dimensional arrays. After creating a two-dimensional array, I then put it into a class and started playing with it. You can see it in the following listing:

<?phpclass TwoDim
{private$members;publicfunction __construct(){$this->members=array(array(os =>'Mac', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'PHP'),array(os =>'PC', program =>'PHP'),array(os =>'PC', program =>'jQuery'),array(os =>'PC', program =>'PHP'),array(os =>'Mac', program =>'jQuery'));}publicfunction getArray(){return$this->members;}}?>

I wasn’t thinking at all about the Template Method, but when I decided it’d be interesting to do a Chi Square test, I found that the Template Method was both helpful and appropriate. Breaking down the steps in a Chi Square test for a 2 x 2 table is pretty easy:

Get the observed cell values (O)

Get the column and row totals

Determine the expected frequencies (E)

Calculate the Chi Square value by adding up (O-E)2 / E

Compare the Chi Square value to values in a probability table where df=1 to determine the p value

Those steps must be taken in the order indicated. The exact details of how that is going to be done can vary depending on the data coming in; but the order is set because each step depends on data from the previous step. This is where the template method comes in. It establishes the order in which operations will occur.

Assuming that the client class will read the data from the array to establish the four cell values (O values), the Template Method pattern sets up the sequence for calculating the Chi Square.

The methods within the templateMethod() method are abstract. What that means when using the templateMethod() method, the enclosed methods can be implemented in any way necessary. However, they must be executed in the order set up by thetemplateMethod(). The interface for the Template Method design is an abstract class. The following listing shows the entire class (IChiSquare) housing the templateMethod().

The great bulk of the ChiSquare class is used up implementing the four key methods from the IChiSquare abstract class. The doX2() method passes arguments from the client and returns the values generated back to the client. The bundleArray() method places all of the calculated value into a one-dimentional associative array so that it can be used by the client to create the table and chi square and probability values.

For me, one of the coolest features of this pattern and the ChiSquare class in particular is how the templateMethod() is called. All of the methods to calculate chi square and the required elements for the table are generated by the implemented methods. So the same pattern could be reused for different tables, requiring only that the methods be expanded to include the table dimensions. As it is, it can handle any 2 X 2 table (df=1). All it needs is the values of the four cells.

The Hard Working Client

Generally, I prefer the client class to do as little as possible other than making requests. In this case, the client class, ChiSqClient is worked harder than a rented mule. It first has to calculate the cell values from the two-dimensional array, and then it passes the cell values to the ChiSquare class and gets the returned values. Finally, it has to build and display a table using the appropriate values. Helper classes could deal with some of that work, but in this case it seemed that a single client class would make it a little easier to see what’s going on.

The Template Method design pattern has Swiss Army Knife features in that it is simple yet has many uses. If you have Learning PHP Design Patterns, take a look at Chapter 9 for more details about this handy pattern; especially the materials on the “Hollywood Principle.” In the meantime, play with this implementation and let me know if you have ideas for improvement.