The relationship between classes and classes in UML class diagrams (class Diagram) and their presentation

Source: Internet
Author: User
Tags abstract inheritance

There are roughly 5 relationships between classes:

1, Dependencies (Dependency)

One-way, which means that a class relies on the definition of another class, where a change in one class affects another class, 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 Card
		= new card ();//local variable
		...
	}
}
public class Person {public
	void DoSomething (Card card) {//Method parameters
		...
	}
}
public class Person {public
	void dosomething () {
		int id = card.getid ();//static method call
		...
	}
}

2, association Relationship (Association)

One-way or bidirectional (usually we need to avoid using bidirectional association relations), is a "has a" relationship, if a one-way association B, you can say 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, polymerization relationship (Aggregation)

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

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

public class Team {public person person
	;
	
	Public team (person) {
		This.person = person;
	}
}

4, combination relationship (composition)

One-way, is a strong dependence of a special aggregation relationship

The Head,body,arm and leg groups synthesize people, and their life cycle is the same, and if the whole does not exist, parts will die out.

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 the parent class, which belongs to this relationship

Can be divided more carefully:

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

Generalization (generalization): The "is a" relationship, the class inherits the abstract class, the class inherits the parent class all belong to this 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.