03.09.2015 Views

Design Patterns

Download - Assembla

Download - Assembla

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 9 ■ THE COMPOSITE PATTERN 127<br />

Example: Form Validation<br />

In this example, let’s say you get a new project at work. Initially it seems simple: create a form<br />

whose values can be saved, restored, and validated. Any half-rate web developer could pull this<br />

off, right? The catch is that the contents and number of elements in the form are completely<br />

unknown and will change from one user to the next. Figure 9-2 shows a typical example.<br />

A validate function that is tightly coupled to specific form fields, such as Name and Address,<br />

won’t work because you won’t know at development time what fields to look for. This is a perfect<br />

job for the composite.<br />

Figure 9-2. Each user can potentially see a different form.<br />

First, let’s identify the elements of a form and label them as either a composite or a leaf<br />

(see Figure 9-3 for the identification). The most basic building blocks of a form are the fields<br />

where the user enters data: input, select, and textarea tags. Fieldset tags, which group related<br />

fields together, are one level up. The top level is the form itself.<br />

Figure 9-3. Identifying the basic form elements as composite or leaf

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!