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

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…

Contents