课程: Programming Foundations: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Solution: The Adapter pattern

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,…

内容