ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Extended Example: Color Printing | 83

Clients


Clients need to instantiate the new print center classes to access the color printers.


Theprint( )method takes a parameter that specifies a color or black and white print


job. They operate on different print job objects based on the passed parameters.


var pcNewHighVol:NewPrintCenter = new NewHighVolPrintCenter( );
var pcNewLowhVol:NewPrintCenter = new NewLowVolPrintCenter( );

pcNewHighVol.print("LongThesis.doc", NewHighVolPrintCenter.BW);
pcNewHighVol.print("SalesReport.pdf", NewHighVolPrintCenter.COLOR);
pcNewLowhVol.print("ShortVita.doc", NewLowVolPrintCenter.BW);
pcNewLowhVol.print("SalesChart.xlc", NewLowVolPrintCenter.COLOR);

The old print center classes,LowVolPrintCenter and HighVolPrintCenter, which


implement a non-parameterized factory method, will continue to work. They’ll con-


tinue to use theWorkgroupPrintjobandInkjetPrintjobprint job classes without dis-


ruption. The power of the factory method design pattern to handle changing


requirements is very evident in this example.


Parallel Class Hierarchies


Take a look at the class diagram for the extended print center application


(Figure 2-7). You will see two distinct concrete product class hierarchies. The


WorkgroupPrintjobandColorLaserPrintjobclasses represent the high volume print


jobs. Likewise, theInkjetPrintjobandColorInkjetPrintjobclasses represent the low


volume print jobs. In addition, knowledge about these class hierarchies is encapsu-


lated within their corresponding creator classes. Note that the product classes can-


not be accessed directly. They can only be accessed through the creator classes. The


NewLowVolPrintCenterandNewHighVolPrintCenterknow about their corresponding


product class hierarchy. We can think of the factory method pattern as sets ofparal-


lel class hierarchies: the concrete creator classes and their corresponding products.


This is a powerful way of encapsulating knowledge and managing dependencies


within software applications.


Figure 2-7. Class diagram for the extended print shop example


Printjob

InkjetPrintjob
ColorInkjetPrintjob

InkjetPrintjob
ColorInkjetPrintjob

NewPrintCenter
print()
createPrintjob()

NewLowVoIPrintCenter
createPrintjob()

NewHighVoIPrintCenter
createPrintjob()
Free download pdf