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.

it. Extending the existing class allows the adapter to inherit its properties and<br />

methods.<br />

As is evident from the class diagram, implementing a class adapter requires multiple<br />

inheritance. Multiple inheritance is an OOP feature that enables a class to inherit<br />

from more than one superclass. In Figure 5-3, the Adapter class inherits from both<br />

the Target and the Adaptee classes.<br />

Figure 5-3. Class diagram of class adapter<br />

According to the classic treatise on design patterns written by the group affectionately<br />

known as the Gang of Four (GoF), one inheritance branch of a class adapter is<br />

used to inherit the interface, and the other to inherit implementation. They also point<br />

out that in most cases, the class adapter inherits the interface publicly while the<br />

implementation branch is inherited privately. This hides the implementation branch<br />

of the inheritance from public view, resulting in the desirable situation where only<br />

the public interface is left visible to clients. In Figure 5-3, the Adapter class would<br />

inherit publicly from the Target class and privately from the Adaptee class. This<br />

implies that the Adapter would be a subclass of Target, and it would make use of<br />

Adaptee methods and properties to implement required operations.<br />

Implementing multiple inheritance in a programming language is not an easy task,<br />

and <strong>ActionScript</strong> <strong>3.0</strong> doesn’t support it. Therefore, implementing a classic class<br />

adapter is not possible using <strong>ActionScript</strong> <strong>3.0</strong>. However, an <strong>ActionScript</strong> <strong>3.0</strong> class<br />

can subclass and implement an interface at the same time. If the required interface is<br />

defined at ITarget, then the class declaration will be:<br />

public class Adapter extends Adaptee implements ITarget<br />

The Adapter class will implement the ITarget interface and inherit from the Adaptee<br />

class at the same time. The big difference is that inheritance of Adaptee will be public,<br />

exposing its functionality. Although not a pure class adapter, this implementation<br />

does have its uses and can be desirable in certain contexts.<br />

Minimalist example of a class adapter<br />

5-5 through Example 5-8 show the implementation of a class adapter in Action-<br />

Script <strong>3.0</strong>. The different files are called: Adaptee.as (existing class), ITarget.as<br />

(required interface), Adapter.as (adapter), and Main.as (client).<br />

182 | Chapter 5: Adapter Pattern<br />

Target<br />

request()<br />

Adapter<br />

request()<br />

specificRequest()<br />

Adaptee<br />

specificRequest()

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

Saved successfully!

Ooh no, something went wrong!