From the course: Programming Foundations: Design Patterns

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Solution: The Adapter pattern

Solution: The Adapter pattern

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Solution: The Adapter pattern

(electronic music) - [Narrator] Here's our solution for the drone adapter. Remember that the adapter needs to implement the interface of the object it's adapting to. Which in this case, is the duck. So the adapter must implement the two duck interface methods quack and fly. The drone adapter takes a drone. The quack method of the drone adapter delegates to the beep method of the drone. So the drone makes a sound. The fly method of the drone adapter is a little more complex. To fly the drone, we actually need to call two methods: spin rotors and take off. So one method in the duck interface corresponds to two method calls in the drone's interface. Now that we have the drone adapter that implements the duck interface, we can add the drone to the duck simulator. Here's how we do that. We first create a drone and then create a drone adapter, passing the drone to the adapter. What we get back is a drone adapter, but remember, it's also a duck, since the adapter implements the duck…

Contents