ActionScript 3.0 Design Patterns

(Chris Devlin) #1

190 | Chapter 5: Adapter Pattern


The Adapter Class


We now have a target interface. The next step is to figure out if an object or class


adapter is the most suitable option. Because the existing class is small with only two


public methods, we won’t get any benefits from using a class adapter. If there were


methods in the existing class that could be directly used by the client in the new con-


text, a class adapter would make sense. Therefore, the CarAdapter shown in


Example 5-11 will be implemented as an object adapter.


Example 5-10. ICar.as


package
{
public interface ICar
{
function start( ):void
function turnMoreToTheLeft( ):void
function turnMoreToTheRight( ):void
function goStraight( ):void
}
}


Figure 5-5. The interface showing keyboard input


Example 5-11. CarAdapter.as


package
{
public class CarAdapter implements ICar
{


private var legacyCar:LegacyCar;
private var nSteeringAngle:Number;


Ignition key

function start() :void

function goStraight() :void

Up arrow key

Left arrow key Right arrow key

function turnMoreToTheRight() :void

function turnMoreToTheLeft() :void
Free download pdf