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.

methods and properties to ac<strong>com</strong>plish what it needs to do. The ITarget interface<br />

could declare methods that are needed in the new context, but have no relation to<br />

the existing class.<br />

Key OOP Concepts in the Adapter Pattern<br />

Comparing the advantages and disadvantages of object and class adapters enables us<br />

to clearly understand the merits of <strong>com</strong>position versus inheritance in OO (objectoriented)<br />

design.<br />

Object and Class Adapters Compared<br />

Choosing between object and class adapters depends on the context in which they<br />

are used. Object adapters that use parameterized constructors are flexible because<br />

the Adaptee object is passed to them. This decouples the Adaptor and Adaptee classes,<br />

allowing instances of subclasses of Adaptee class to be passed to the adapter.<br />

For their part, class adapters generally have less code, resulting in quicker implementation.<br />

Because they subclass the Adaptee, they have an is-a relationship with the<br />

Adaptee class. The Adapter class is therefore the same Type as the Adaptee, and easier<br />

to deal with from a client standpoint. Clients need to create only one instance of the<br />

Adapter class (as opposed to both an Adaptee and Adapter instance, in the case of<br />

parameterized object adapters). Class adapters can also be less time-consuming to<br />

implement if the target interface is large, because most methods will be inherited.<br />

The decision to choose either an object or adapter class to implement an adapter<br />

depends on many factors: flexibility required, the size of the interface, ease of implementation,<br />

and match between the required interface and the Adaptee.<br />

As a general rule, <strong>com</strong>position is better than inheritance because of its flexibility and<br />

loose coupling. However, the adapter pattern shows that there can be instances<br />

where you can save time and gain coding efficiency by using inheritance.<br />

Example: Car Steering Adapter<br />

Adapter patterns are <strong>com</strong>monly used when there is an existing legacy class that provides<br />

the functionality you require, but whose interface doesn’t conform to what you<br />

need. Let’s assume that we have a legacy class called LegacyCar, which is a car sprite<br />

from an old game. The LegacyCar class was initially developed for use with a steering<br />

wheel input device to provide an authentic driving experience. We will change the<br />

interface to the LegacyCar class so that we can steer the car using different input<br />

devices such as a keyboard and mouse.<br />

Example: Car Steering Adapter | 185

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

Saved successfully!

Ooh no, something went wrong!