Sağlıksız Dünyamız

Page 178

180

The Specification Pattern

foreach($this-> >destinations as $dest) { if ($warm_enough-> >isSatisfiedBy($dest)) { $valid_destinations[] = $dest; } } $this->assertEqual(1, count($valid_destinations)); $this->assertIdentical( $this->destinations[‘Cancun’], $valid_destinations[0]); } }

You can see how a Parameterized Specification gives you an extra degree of freedom. Let’s look at another example where the data type and the Specification are extremely flexible by necessity. One of the most common and most maddening problems to solve in a web application is validating form input. Forms tend to change during development (and even beyond) and the number of forms in a rich application can grow rather quickly. You could create a unique object to encapsulate each and every form and use the Specification pattern to validate each object, but that’s a maintenance nightmare. Is there a convenient data type that can adapt readily to any form? If so, is there a way to validate such a dynamic data type? The answer to each of those questions is an emphatic yes. The Web Application Component Toolkit’s (WACT) DataSource interface can get, set, and dynamically create object properties (something akin to PHP4’s __get() and __set() methods), which are a handy encapsulation of a form. (Readers familiar with Java can think of a DataSource as a HashMap.) Meanwhile, the Parameterized Specification pattern provides a model to validate a DataSource against a set of criteria.

L

WACT WACT, the Web Application Component Toolkit, available on SourceForge at http://wact.sf.net/, is a library of PHP code for solving common web application problems. WACT focuses heavily on techniques of refactoring, unit testing, and design pattern use. Information related to the WACT concept of a DataSource is located at http://wact.sf.net/index.php/DataSource. The include file for the WACT DataSource class is included in this book’s source code download so you can test the policy code.

For this example, the DataSource class can be thought of as the following code, which is nearly iden-


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.