10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ignition key of the car (assume that the car is in gear all the time). The<br />

setSteeringWheelAngle( ) public method takes in the angle of the steering wheel and<br />

rotates the car accordingly. The interface is illustrated in Figure 5-4.<br />

Ignition key<br />

Steering wheel<br />

public function start() :void<br />

Figure 5-4. The original interface of the LegacyCar class<br />

public function setSteeringWheelAngle (nAngle:int) :void<br />

The new context<br />

Let’s assume that the new context requires the car to be driven, not only by a steering<br />

wheel, but by alternative input devices as well. For example, we need to drive the<br />

car using the keyboard or the mouse. Think of the new context as an effort to modify<br />

the car to be driven by someone disabled who can’t use a steering wheel. The<br />

original interface allowed the car to be steered with very fine-grained movements<br />

using the setSteeringWheelAngle( ) method. You could make the steering wheel turn<br />

half a degree or 900 degrees (2.5 <strong>com</strong>plete rotations of the steering wheel). However,<br />

generating such a wide range of values is not possible with key presses. A key<br />

press essentially has two values, key down and key up. It is evident that we’ll have to<br />

turn the steering wheel in discrete steps. For example, we can assume that every<br />

press of the left-arrow key represents turning the steering wheel by 50 degrees counterclockwise.<br />

The right-arrow key will, in turn, represent turning the steering wheel<br />

in the clockwise direction. It will also be helpful to have a way to get the steering<br />

wheel back to its original position. The up arrow key can be used to set the steering<br />

wheel angle to zero. We need to convert the interface to fit this new context.<br />

The new interface<br />

Based on the previous analysis, the new interface needs several methods to meet the<br />

requirements of the new context. The ICar target interface, shown in Example 5-10<br />

and depicted in Figure 5-5, is designed for the new context.<br />

Example: Car Steering Adapter | 189

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

Saved successfully!

Ooh no, something went wrong!