ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Example: Car Steering Adapter | 189

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


setSteeringWheelAngle( )public method takes in the angle of the steering wheel and


rotates the car accordingly. The interface is illustrated in Figure 5-4.


The new context


Let’s assume that the new context requires the car to be driven, not only by a steer-


ing wheel, but by alternative input devices as well. For example, we need to drive the


car using the keyboard or the mouse. Think of the new context as an effort to mod-


ify the car to be driven by someone disabled who can’t use a steering wheel. The


original interface allowed the car to be steered with very fine-grained movements


using thesetSteeringWheelAngle( )method. You could make the steering wheel turn


half a degree or 900 degrees (2.5 complete rotations of the steering wheel). How-


ever, generating such a wide range of values is not possible with key presses. A key


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


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


press of the left-arrow key represents turning the steering wheel by 50 degrees coun-


terclockwise. The right-arrow key will, in turn, represent turning the steering wheel


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


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


wheel angle to zero. We need to convert the interface to fit this new context.


The new interface


Based on the previous analysis, the new interface needs several methods to meet the


requirements of the new context. TheICartarget interface, shown in Example 5-10


and depicted in Figure 5-5, is designed for the new context.


Figure 5-4. The original interface of the LegacyCar class


Ignition key

public function start() :void

Steering wheel

public function setSteeringWheelAngle (nAngle:int) :void
Free download pdf