| 1 |
Wormhole Example |
| 2 |
================ |
| 3 |
|
| 4 |
An example of using the wormhole aspect. Shows how the design degrades |
| 5 |
when a feature is added, and how refactoring to aspects improves the |
| 6 |
design. |
| 7 |
|
| 8 |
|
| 9 |
before.pl - the initial design. Our company prints documents. We have |
| 10 |
the classes Printer, SpamPrintJob, and SpamDocument. To |
| 11 |
print a job you create it, create a Printer, and call |
| 12 |
$printer->print($job). The call flow will be (Class::method): |
| 13 |
|
| 14 |
Printer::print |
| 15 |
SpamPrintJob::spool |
| 16 |
SpamDocument::spool |
| 17 |
|
| 18 |
All is well: the design is nice and clean, and allows us |
| 19 |
to create a hierarchy of classes for different jobs and |
| 20 |
different documents. |
| 21 |
|
| 22 |
|
| 23 |
after_oop.pl - we are told the company has purchased several new |
| 24 |
printers. This means that SpamDocument::spool must get |
| 25 |
the name of the printer from somewhere. And it must be |
| 26 |
the name of the printer that is currently printing this |
| 27 |
document. So we append the Printer object to the argument |
| 28 |
list of each method in the call flow. SpamPrintJob now |
| 29 |
suffers from EEK. It knows about the fact that SpamDocument |
| 30 |
needs a Printer. |
| 31 |
|
| 32 |
|
| 33 |
after_aop.pl - to remove EEK we refactor to use the Wormhole aspect. |
| 34 |
SpamPrintJob now knows nothing of the fact that we have |
| 35 |
several printers. In fact, the code is exactly like |
| 36 |
before.pl, except for these changes: |
| 37 |
|
| 38 |
* added to Printer instance data for name support |
| 39 |
* added a wormhole aspect |
| 40 |
* appended a Printer argument to SpamDocument::spool |
| 41 |
|
| 42 |
The wormhole allows us to pass the Printer down a call |
| 43 |
flow, without modifying any objects along the way. |
| 44 |
|
| 45 |
|