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.

fit an adult’s bottom, and we now need to convert that interface to fit a toddler’s<br />

smaller bottom. We need an adapter that presents the correct interface to fit the current<br />

context. The toilet seat adapter was built to do precisely that. We get to use an<br />

existing object whose interface has been converted to one that the client expects.<br />

Existing object<br />

Toilet<br />

Figure 5-1. Toilet seat adapter<br />

How is this analogous to an adapter pattern? We have an existing object (toilet seat);<br />

a new interface it needs to conform to (a toddler’s bottom); an adapter that converts<br />

the interface (toilet seat adapter); and a new client (toddler).<br />

Let’s extend this analogy to <strong>com</strong>puter code. The existing object would be analogous<br />

to an existing class that creates objects. The new interface would represent new<br />

requirements for this class. The adapter would be another class that implements the<br />

new interface by using the existing class.<br />

What would be the alternative to using an adapter in this case? We could rewrite or<br />

modify the code in the existing class to fit the new interface. This is analogous to<br />

buying one of those small potties built for toddlers (with no plumbing). I think most<br />

people would rather not have to deal with the additional cost and the disposal issue.<br />

This can be as painful as changing existing code that already works, introducing<br />

bugs, and breaking dependencies. An adapter would be a better option in both cases.<br />

Key Features of the Adapter Pattern<br />

Before considering an adapter pattern for a project, a couple conditions should exist:<br />

• An existing class that meets some of the implementation requirements, but<br />

whose interface is in<strong>com</strong>patible in the new context of use<br />

• A target interface for the new context<br />

If these conditions exist, then we can develop an adapter class that uses the existing<br />

class to implement the target interface. The new context should be thoroughly analyzed,<br />

and the new requirements defined, before thinking about implementation. In<br />

other words, we should know the desired interface before we look at existing classes<br />

178 | Chapter 5: Adapter Pattern<br />

Adapter<br />

Toilet seat<br />

adapter<br />

Client<br />

New context

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

Saved successfully!

Ooh no, something went wrong!