ActionScript 3.0 Design Patterns

(Chris Devlin) #1

182 | Chapter 5: Adapter Pattern


it. Extending the existing class allows the adapter to inherit its properties and


methods.


As is evident from the class diagram, implementing a class adapter requiresmultiple


inheritance. Multiple inheritance is an OOP feature that enables a class to inherit


from more than one superclass. In Figure 5-3, theAdapterclass inherits from both


theTarget and theAdaptee classes.


According to the classic treatise on design patterns written by the group affection-


ately known as the Gang of Four (GoF), one inheritance branch of a class adapter is


used to inherit theinterface,and the other to inheritimplementation. They also point


out that in most cases, the class adapter inherits the interfacepubliclywhile the


implementation branch is inheritedprivately. This hides the implementation branch


of the inheritance from public view, resulting in the desirable situation where only


the public interface is left visible to clients. In Figure 5-3, theAdapterclass would


inherit publicly from theTargetclass and privately from theAdapteeclass. This


implies that theAdapterwould be a subclass ofTarget,and it would make use of


Adaptee methods and properties to implement required operations.


Implementingmultiple inheritancein a programming language is not an easy task,


and ActionScript 3.0 doesn’t support it. Therefore, implementing a classic class


adapter is not possible using ActionScript 3.0. However, an ActionScript 3.0 class


can subclass and implement an interface at the same time. If the required interface is


defined atITarget, then the class declaration will be:


public class Adapter extends Adaptee implements ITarget

TheAdapterclass will implement theITargetinterface and inherit from theAdaptee


class at the same time. The big difference is that inheritance ofAdapteewill be pub-


lic, exposing its functionality. Although not a pure class adapter, this implementa-


tion does have its uses and can be desirable in certain contexts.


Minimalist example of a class adapter


5-5 through Example 5-8 show the implementation of a class adapter in Action-


Script 3.0. The different files are called: Adaptee.as (existing class), ITarget.as


(required interface),Adapter.as (adapter), andMain.as (client).


Figure 5-3. Class diagram of class adapter


specificRequest()

Target
request()

Adaptee
specificRequest()

Adapter
request()
Free download pdf