Re-reading the study notes of design patterns (1) -- mixed classes

Source: Internet
Author: User

The author mentioned the mixed class in 1.6.4, but its description is very simple: the mixed class provides alternative interfaces or functions for other classes. It cannot be instantiated like an abstract class. The mixed classes require inheritance. The following class diagram is provided:

 

I think mixed classes are mainly used when different classes have some identical interfaces. The use of mixed classes not only improvesCodeThe reusability of the Code improves the maintainability of the Code.
Now we use a simple example to illustrate the advantages of mixed classes in code reusability.
For example, there is a personal financial processing system that collects statistics on a person's monthly income and expenses and displays them in the form of reports. We can write two abstract classes for income and expenditure: cincome and cexpenditure. Of course, we can add a createreport () method to the two classes to generate their respective reports. The class diagram is as follows:

In this case, we have to add the same createreport () method to both classes. However, once the report generation method is changed, we must modify the code of the createreport () method in the cincome class and cexpenditure classes respectively; if we have added the automatic report sending function for the system, we have to add a sendreport () method to the cincome class and cexpenditure class respectively. This not only wastes time but also reduces the maintainability of the Code. At this time, we can add a mixed class creport. The class diagram is as follows:

After the new inheritance system is adopted, you do not have to modify the cincome and cexpenditure classes if you have any changes to the report implementation functions or add any new functions. The use of mixed classes improves code reusability and maintainability.

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.