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.
Solution: The Strategy pattern
From the course: Programming Foundations: Design Patterns
Solution: The Strategy pattern
(energetic music) - [Narrator] Here's our solution to the challenge. We've pulled out the different ways of sharing into a separate family of classes that implement sharing by texting, sharing by email, and sharing on social media. This is our family of algorithms. We've also added a property to the PhoneCameraApp superclass that will be composed with a strategy that the user selects. Now when the share method is called, the super class is going to delegate sharing to the user's preferred share strategy. So this design allows us to more easily add, change, and remove sharing strategies without have to modify any code in the camera app or any code in the concrete camera app classes.
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
-
-
-
-
-
-
-