From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 24,100 courses taught by industry experts.
Applying the principles
From the course: Programming Foundations: Design Patterns
Applying the principles
- [Instructor] Let's take a look at some sample code to demonstrate how we implement our new design and pull out and separate what varies in our code. For this sample code, we'll just Java. We'll be showing just a few snippets of code. To see the full working examples, download the exercise files. We'll begin by looking at the flying and quacking behaviors we've pulled out from the ducks. Recall one of the design principles we've talked about. Program to an interface, not an implementation. For both our behaviors, we'll define interfaces that all behaviors will implement. So, all fly behaviors will implement the flyBehavior interface. For example, we might implement a FlyWithWings behavior that implements regular flying and a FlyNoWay behavior that implements no flying for a duck that can't fly. Likewise all quack behaviors will implement the quack behavior interface. We might implement quacking behaviors like…
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
-
-
-
-
-
-
-