From the course: Programming Foundations: Design Patterns

Unlock the full course today

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

Challenge: The Strategy pattern

Challenge: The Strategy pattern

From the course: Programming Foundations: Design Patterns

Start my 1-month free trial

Challenge: The Strategy pattern

(energetic music) - [Instructor] Take a look at the class diagram on this slide. We have an abstract class, PhoneCameraApp, with four methods: take, edit, save and share. And, we have two concrete classes that extend PhoneCameraApp. BasicCameraApp, and CameraPlusApp. The edit method in the superclass is abstract, because each camera app will provide its own specialized editor, but the rest of the methods can be inherited by the subclasses. We get a lot of code reuse, because the taking, saving and sharing are the same across many apps. That way, the apps can implement custom editors that get the other functionality for free from the superclass. The share method in the superclass lets the user pick how they want to share the photo, and then uses that algorithm to share. Right now, the share method allows the user to share photos by text and by email. We're running into a problem, however. We keep having to open up the PhoneCameraApp class to modify the share method every time a new way…

Contents