Dynamic Selection of Concrete Components and Decorations: A Hybrid Car Dealership | 175
Implementing the concrete components and their decorators
The pattern implementation process requires that certain things are in place so that
the user can choose what she wants as far as both concrete components (car model)
and concrete decorators are concerned. Initially, the script must first find which car
has been selected. So the functiongetCar( )loops through the radio buttons until it
finds which one is selected. When it locates it, it simply assigns an instance of
carDeal with one of the concrete components such as:
carDeal = new Mariner( );
ThecarDealobject has to be instantiated outside a function so that it can be used by
more than a single private function. On line 16 of Example 4-48, thecarDealobject
is instantiated as anAutodata type. (Theinternalstatement is added to draw atten-
tion to the fact that the variable is available to any caller in the same package—even
though it is the default state when instantiating variables.)
Once thecarDealobject has been created, the script looks at the options selected.
Using thegetOptions( )function, each of the checkboxes is compared to its selec-
tion state. When a selected option is found, the option, a concrete decorator, wraps
the concrete objectcarDeal. For example, if one of the selections is heated seats, the
code section in the switch statement looks like the following:
case "Heated Seats":
carDeal = new HeatedSeats(carDeal);
break;
Figure 4-8. Decorator user interface and output