O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object and Class Adapters

An adapter can exploit an existing class to implement functionality in two ways. The adapter can use either composition or inheritance to access an existing class. If the adapter subclasses the existing class, it will inherit the methods and properties. If the adapter uses composition to access the existing class, the adapter class will hold a reference to an instance of the existing class, and use its methods and properties for implementation. In OOP, composition is generally preferred over inheritance. However, as we will see in subsequent examples, there are good arguments for both approaches depending on the context. Either way, the idea is to use the properties and methods of the existing class to provide most or some of the functionality required.

Object Adapters

When the adapter uses composition to access the existing class, it's known as an object adapter. Object adapters store a reference to an instance of the existing class. Therefore, an object adapter uses an existing class.

All adapter classes implement an interface. In the class diagram shown in Figure 5-2, the interface called ITarget declares a single method called request(). The class diagram shows that a reference to an instance of the Adaptee class is stored in the variable adaptee. When implementing the request() method, the adapter references this instance.

Class diagram of an object adapter

Figure 5-2. Class diagram of an ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required