From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Programming to an interface
From the course: Programming Foundations: Design Patterns
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Revisiting inheritance1m 30s
-
(Locked)
Limitations of inheritance4m 18s
-
(Locked)
Trying interfaces2m 28s
-
(Locked)
Get inspiration from design principles3m 37s
-
(Locked)
Programming to an interface1m 32s
-
(Locked)
Applying the principles4m 36s
-
(Locked)
Exploring the strategy pattern1m 59s
-
(Locked)
Why HAS-A is better than IS-A2m 4s
-
(Locked)
Challenge: The Strategy pattern2m
-
(Locked)
Solution: The Strategy pattern46s
-
-
-
-
-
-
-
-