Design Pattern principle-synthesis/aggregation Reuse Principle (IV)

Source: Internet
Author: User

  Reading directory

I. Principles of synthesis/aggregation multiplexing

Ii. What is synthesis?

3. What is aggregation?

4. Why not use synthesis/aggregation instead of class inheritance?

V. Principle structure of merging/aggregation multiplexing

  I. Principles of synthesis/aggregation multiplexing

  Use merging/aggregation whenever possible, and do not use class inheritance whenever possible

 Ii. What is synthesis?

  .Synthesis represents a strong relationship of ownership, reflecting the strict relationship between the part and the whole, and the same as the overall life cycle. For example, a person has two arms, the arm and the person are the relationship between the part and the whole. If a person dies, the arm will be useless. That is to say, the arm and the person's life cycle are the same.

  .The compositing relationship is expressed by solid diamond + solid line.

  3. What is aggregation?

  .Aggregation indicates A weak relationship of ownership. Object A can contain object B, but object B is not part of object A. For example, A person is A group animal, therefore, each person belongs to a group. A group can have multiple people, so the group and people are aggregated.

  .The aggregation relationship is represented by a hollow diamond + solid line.

 4. Why not use synthesis/aggregation instead of class inheritance?

  .The Object Inheritance relationships are defined at compilation, so the implementation of child classes inherited from the parent class cannot be changed at runtime.

.The implementation of a subclass is closely related to its parent class, so that any changes in the implementation of the parent class will inevitably lead to changes in the subclass.

  .When you reuse child classes, if the inherited implementation is not suitable for solving new problems, the parent class must be overwritten or replaced by other more suitable classes.

This dependency limits flexibility and ultimately limits reusability.

V. Principle structure of merging/aggregation multiplexing

  1, 2, and n are referred to as the base number, indicating that the class at this end can have several instances. Obviously, a person has two arms. If a class may have multiple instances, n indicates that there may be countless people in a group.

  

 

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.