课程: Programming Foundations: Design Patterns

今天就学习课程吧!

今天就开通帐号,24,100 门业界名师课程任您挑!

Why HAS-A is better than IS-A

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…

内容