课程: Programming Foundations: Design Patterns
今天就学习课程吧!
今天就开通帐号,24,100 门业界名师课程任您挑!
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…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
Revisiting inheritance1 分钟 30 秒
-
(已锁定)
Limitations of inheritance4 分钟 18 秒
-
(已锁定)
Trying interfaces2 分钟 28 秒
-
(已锁定)
Get inspiration from design principles3 分钟 37 秒
-
(已锁定)
Programming to an interface1 分钟 32 秒
-
(已锁定)
Applying the principles4 分钟 36 秒
-
(已锁定)
Exploring the strategy pattern1 分钟 59 秒
-
(已锁定)
Why HAS-A is better than IS-A2 分钟 4 秒
-
(已锁定)
Challenge: The Strategy pattern2 分钟
-
(已锁定)
Solution: The Strategy pattern46 秒
-
-
-
-
-
-
-