-Head first design pattern-master and disciple (3)-favor composition over inheritance

Source: Internet
Author: User
MASTER:Oh, grasshopper. I have talked about it before. Forgive me, but it has been a while. Do you fully understand inheritance in OO?
Disciples:Yes, master. The power of "oo inheritance" is not what I say. Later on, I became familiar with my knowledge and found that "oo inheritance" was not a great solution, and it was not always possible to design a solution that was easy to scale and maintain. Code!
MASTER:Well, it seems that you have grown a lot. Tell me, my dear grasshopper, how can I reuse it without "inheritance? Copy?
Disciples:Ha, I have a joke. Master, I think that at runtime, you can use combination and delegation to replace "inheritance ".
MASTER:Go on...
Disciples:In the past, we used the method of inheriting the parent class in the subclass. during compilation (compile time), all the behaviors were fixed! In addition, all inherited child classes have repeated behaviors. Now, if we extend the class behavior through a combination of methods, it is easy to use these behavior at runtime!
MASTER:Very good, grasshopper. You have begun to understand the power of the combination method.
Disciples:Yes. Through this technology, the generated objects not only include all the features of the parent class (which we may have never imagined), but also do not need to modify the code of the parent class, you can add more functions to the object!
MASTER:So what have you learned to maintain your code through a combination of methods?
Disciples:Combination, Hum. I don't have to modify the original code. To add new functions, I will write new code.
Because I have not modified the original code, the impact of the Bug generation and the unintentional impact on the previous code is greatly reduced.
MASTER:Very good. Today we are here, grasshopper. I hope you will continue to study this topic... Remember, the Code must be closed for modifications, just like sleeping lotus in the evening. The code must be open for extensions, just like sleeping lotus in the morning.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.