课程: Programming Foundations: Design Patterns
今天就学习课程吧!
今天就开通帐号,24,100 门业界名师课程任您挑!
Why HAS-A is better than IS-A
- [Instructor] Let's take a step back, for a second, and look what we've done in the design of the duck simulator. One thing we've done is we're now using a HAS-A relationship to associate a duck with it's flying and quacking behavior. Remember, IS-A is an inheritance relationship. A mallard duck is a duck. And HAS-A is a relationship of composition. A mallard duck has a fly behavior. So now, instead of ducks inheriting their fly behavior, we're now composing the ducks with a fly behavior. Same with a quacking behavior. So keep in mind, that when you put two classes together, with composition, instead of inheriting behavior, an object can then instead delegate that behavior, to the composed object. We now see that composition results in a more flexible design. That let's us easily reuse flying and quacking behavior. This idea, of using composition rather than inheritance, is another important design principle. This…
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
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 秒
-
-
-
-
-
-
-