The relationship between classes and classes in UML class diagrams (classes Diagram) and how they are represented

Source: Internet
Author: User
Tags abstract inheritance

There are roughly 5 types of relationships between classes:

1, dependency relationship (Dependency)

One-way, which indicates that one class relies on the definition of another class, where the change of one class affects the other class, and is a "use a" relationship

If a depends on B, then B behaves as a local variable, method parameter, static method call, etc.

public class Person {public
	void dosomething () {
		card = new Card ();//local variable
		....
	}
}
public class Person {public
	void DoSomething (card) {//method parameter
		....
	}
}
public class Person {public
	void dosomething () {
		int id = card.getid ();//static method call
		...
	}
}

2, association Relationship (Association)

One-way or two-way (usually we need to avoid the use of bidirectional correlation), is a "has a" relationship, if a one-way association B, it can be said a has a B, usually represented as a global variable

public class Person {public
	phone phone;
	
	public void Setphone (phone phone) {		
		this.phone = phone;
	}
	
	Public Phone Getphone () {		
		return phone;
	}
}

3, aggregation relationship (Aggregation)

One-way, the correlation relationship between one, and the relationship between the difference is semantically, the associated two objects are usually equal, aggregation is generally unequal, there is a sense of the whole and local, the implementation of the difference is not small

Class consists of student, its life cycle is different, the whole does not exist, some still exist, the team is now dissolved, people are still, you can join other groups

public class Team {public person person
	;
	
	Public Team {
		This.person = person;
	}
}

4, combinatorial relationship (composition)

One-way, a special aggregation relationship with strong dependence

The Head,body,arm and leg groups synthesize people, whose life cycle is the same, and if the whole is absent, the part will perish.

public class Person {public
	head head;
	public body Body;
	public arm arm;
	Public Leg Leg;
	
	Public person () {
		head = new head ();
		BODY = new Body ();
		ARM = new Arm ();
		Leg = new leg ();
	}
}

5, inheritance Relationship (inheritance)

class implements an interface, a class inherits an abstract class, and a class inherits a parent class that belongs to this relationship

Can be divided into finer points:

Implementation (REALIZATION): Class implementation interface belongs to this relationship

Generalization (generalization): "is a" relationship, class inherits abstract class, class inherits parent class belongs to this kind of relationship

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.