课程: Programming Foundations: Design Patterns

今天就学习课程吧!

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

Programming to an interface

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…

内容