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
From the course: Programming Foundations: Design Patterns
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…
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
-
Limitations of inheritance4m 19s
-
Trying interfaces2m 28s
-
Get inspiration from design principles3m 37s
-
Programming to an interface1m 32s
-
Applying the principles4m 36s
-
Exploring the strategy pattern1m 59s
-
Why HAS-A is better than IS-A2m 4s
-
Challenge: The Strategy pattern2m
-
Solution: The Strategy pattern46s
-
-
-
-
-
-
-