From the course: Programming Foundations: Design Patterns

Unlock the full course today

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

Programming to an interface

Programming to an interface

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Programming to an interface

- [Instructor] Let's go back to the original duck design and rethink this from the perspective of separating what varies and programming to an interface. To separate what varies, we're going to move the implementations of quack and fly out of the duck class. We're also going to make sure that we're programming to an interface rather than an implementation by using two interfaces, quack behavior and fly behavior. Now let's use these interfaces by implementing some concrete quacking and flying subclasses. For quacking, we'll implement a quack subclass, a squeak subclass, and also a class that doesn't make any sound at all, which we'll call mute. For flying, we'll implement a fly with wings subclass and a fly no way subclass. Obviously, we could've added more concrete implementations to both of these. But for now this is all we need. Now that we have these behaviors in place, we can rework the duck class. To do that, we'll add two properties to the duck superclass, a reference to a fly…

Contents