Relationship between classes

Source: Internet
Author: User

There are a lot of discussions about this on the internet, and I find that there are different opinions on this issue, and they are far from each other. By browsing these discussions and referring to o'reilly-UML 2.0 In A Nutshell (2007), I would like to express my views.

There are many types of Inter-class relationships. There are two types of relationships: vertical and horizontal.

A vertical relationship is an inheritance relationship. Its concept is very clear and it has become one of the three important features of OO. This is not discussed here.

The horizontal relationship is more subtle. According to the UML suggestions, there are roughly four types:

  1. Dependency)
  2. Association)
  3. Aggregation)
  4. Composition)

There is no objection to their strong/weak relationship: dependency <Association <aggregation <combination

However, the differences between the four of them are not so good, so you need to understand them well.

  1. Dependency:

    • UML representation: dotted line + arrow
    • Link: "... uses ..."
    • This relationship is the simplest and best understood. The so-called dependency means that the function of an object depends on another object. The dependent object is only used as a tool, but does not hold a reference to it.
    • There are many typical examples, such:
      Class Human
      {
      Public void breath ()
      {
      Air freshAir = new Air ();
      FreshAir. releasePower ();
      }
      Public static void main ()
      {
      Human me = new Human ();
      While (true)
      {
      Me. breath ();
      }
      }
      }

      Class Air
      {
      Public void releasePower ()
      {
      // Do something.
      }
      }

    • Meaning: a person needs to breathe continuously for his own life. The reason why the person's breathing function can maintain his life is that the gas that is sucked in plays a role. Therefore, air is just a tool of mankind, A person does not hold a reference to it.
  2. Association:
    • UML representation: solid line + arrow
    • Link: "... has ..."
    • The so-called association means that an object will hold the reference of another object for a long time, and the association between the two is often mutual. The two objects associated with each other do not have any mandatory constraints. As long as the two agree, they can be removed or associated at any time. They have no agreement on the life cycle issue. The associated object can be associated with another object, so the association can be shared.
    • There are many typical examples, such:
      Class Human
      {
      ArrayList friends = new ArrayList ();
      Public void makeFriend (Human human)
      {
      Friends. add (human );
      }
      Public static void main ()
      {
      Human me = new Human ();
      While (true)
      {
      Me. makeFriend (mySchool. getStudent ());
      }
      }
      }
    • Meaning: People keep making friends from birth to death. However, there is no reason to think that the life and death of friends are related to my life and death, so their life periods are not related, my friends can also be friends of others, so they can share.
  3. Aggregation:
    • UML representation: hollow diamond + solid line + arrow
    • Link: "... owns ..."
    • Aggregation is the association of strong versions. It implies a relationship and a life-cycle relationship. The aggregated objects can be associated with other objects, so the aggregated objects can be shared. Although shared, aggregation represents a more intimate relationship.
    • There are many typical examples, such:
      Class Human
      {
      Home myHome;
      Public void goHome ()
      {
      // On the way home
      MyHome. openDoor ();
      // Watch TV
      }
      Public static void main ()
      {
      Human me = new Human ();
      While (true)
      {
      // Go to school
      // Eat
      Me. goHome ();
      }
      }
      }
    • Explanation: My family and I have a strong relationship with each other. My home can be shared, but there are two types of sharing here. One is the sharing of aggregation, which is as strongly associated with the family as you and your wife, and the other is the sharing of aggregation and association, if your friend comes to the house for a meal, you probably won't give him a key.
  4. Combination:
    • UML representation: solid diamond + solid line + arrow
    • Link: "... is a part ..."
    • A combination is the strongest version of a link. It directly requires that the contained object possess the contained object and the relationship between the contained object and the lifecycle of the contained object. The contained object can be associated with another object, so the contained object can be shared. However, there is no sharing between two contained objects and the same contained object.
    • There are many typical examples, such:
      Class Human
      {
      Heart myHeart = new Heart ();
      Public static void main ()
      {
      Human me = new Human ();
      While (true)
      {
      MyHeart. beat ();
      }
      }
      }
    • Meaning: A composite relationship is the relationship between the whole and the part. The part belongs to the whole, but does not exist. However, the part does not exist, more specifically, some of them must be created after the overall creation and destroyed before the overall destruction. Some objects can be associated or even aggregated by other objects in this life cycle. However, you must note that once some objects are destroyed as a whole, then the reference in the associated object will become a null reference, which can be guaranteed by the program. The life cycle of a heart is the same as that of a person. If you change the life cycle of a person, for example, the appendix. Many people get tired of it at some time after the birth and destroy it in advance, but its relationship with humans is irretrievably a combination.
      In UML, a special case is to allow the object to be included to be transferred to a new object before it is destroyed. Although this is not natural, but it brings the gospel to patients who need heart transplantation.

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.