From the course: Programming Foundations: Design Patterns
Unlock the full course today
Join today to access over 23,400 courses taught by industry experts.
Exploring the strategy pattern
From the course: Programming Foundations: Design Patterns
Exploring the strategy pattern
- [Instructor] We had an initial design for a duck simulator that used inheritance, but we quickly ran into problems with this design. We were having to change a lot of code in the subclasses when we added new ducks and we were duplicating code. We fixed the design by pulling out the flying and quacking behaviors and composing those behaviors with the ducks, that way each duck could use the behaviors that is right for that duck. The class design that we created for the duck simulator is an implementation of the strategy pattern. Let's take a look at the class diagram for the strategy pattern. We have an inheritance hierarchy that defines the type of the objects that need a behavior and we have a HASA relationship between those objects and their behaviors. These behaviors can be anything. Any algorithm that an object might need to perform. By moving these algorithms out from the main inheritance hierarchy, we get the…
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 19s
-
(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
-
-
-
-
-
-
-