ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Program to Interfaces over Implementations | 45

TheMyObjectclass’s sole method isworksForRequest( ). The method is encapsulated,


so it should have only a single way to launch its operations. Fortunately, all we need


to do is create an instance of the class, and add the method to the instance to make it


work, as Example 1-38 shows.


The output tells the whole story:


This was requested by a client

The client in this case is simply an instance of the object whose method it requests.


The client/object relationship is through the request. Often the client adds specific


details through a parameter, but the concept of a request is usually nothing more


than invoking the method with an instance of the object that owns the method.


Program to Interfaces over Implementations


To understand why the authors of design patterns encourage programming to inter-


faces over implementations, you need to first understand the general goal of flexibil-


ity and reusability. Second, you need to appreciate the problem of managing


dependency in large programs.


As we have noted in this chapter, the overall goal of design patterns is to create reus-


able code. In order to meet this overall goal, the code must be flexible. This doesnot


mean that your application runs better or compiles faster. All it does is help you cre-


ate code that you can reuse in other projects. The more time and effort you spend,


the larger the team engaged in working with the code, the more important this over-


all goal.


Managing Dependency


The need for software flexibility leads to the need to manage dependency. When


your code depends on a specific implementation, and the implementation changes,


Example 1-38. MyClient.as


package
{
import flash.display.Sprite;


public class MyClient extends Sprite
{
var myClient:MyObject;
public function MyClient( ):void
{
myClient=new MyObject( );
myClient.worksForRequest( );
}
}
}

Free download pdf