The basic concept of UML "dependency, association, generalization, implementation"

Source: Internet
Author: User

First, the concept:

Introduction to the basic concepts of UML:

UML:UML (Unified Modeling Language) provides a unified, standard, and visual modeling language for object-oriented software design. It is suitable for describing the whole process of software design which is driven by use case and is architecture-centric.


UML Model Diagram is composed of things, relationships and graphs.
1. Things:Things are: Component things "class, interface, use case, component, node, etc." Behavior things "interaction, state machine" group things "package" annotation Things "comment"

2. Relationship: Dependency, association "aggregation, Combination", generalization, implementation
3, Diagram: Class diagram, use case diagram, state diagram, object graph, sequence diagram, collaboration diagram, activity diagram, Component diagram, deployment diagram and so on.
Second, the UML syntax description:"This figure is in the startuml detailed tutorial"

third, the relationship in UML:1, generalization (generalization):is a class (called a subclass, sub-interface) that inherits the functionality of another class (called a parent, parent interface) and can augment its own new functionality, which is the most common relationship between classes and classes or interfaces and interfaces, and in Java such relationships are explicitly identified by the keyword extends.

represented by an implementation with a hollow arrow. "Other relationship representations, see UML syntax descriptors"
The class diagram is described as follows:

the inheritance code for the class is as follows:
Package dim.uml.generalization;/* * ClassA extends ClassB, ClassA is_a classB  */public class ClassA  extends Class B{public ClassA () {//Todo auto-generated constructor stub}}class classb{public ClassB () {//TODO auto-generated construct or stub}}


the inheritance code for the interface is as follows:interface A:
<span style= "FONT-SIZE:14PX;" >package Dim.uml.generalization;public interface Interfacea {void Dosometingina ();} </span>

Interface B: Inherit interface a
<span style= "FONT-SIZE:14PX;" >package Dim.uml.generalization;public Interface Interfaceb extends Interfacea{void dosometinginb ();} </span>

Test interface: You can see that the interface B is inherited, and the Dosomethingina and DOSOMETHINGINB are replicated.
<span style= "FONT-SIZE:14PX;" >package Dim.uml.generalization;public class TestInterface  implements interfaceb{@Overridepublic void Dosometingina () {//Todo auto-generated method stub} @Overridepublic void Dosometinginb () {//Todo auto-generated method St Ub}}</span>


2. Realization (realitization):is a class class that implements the interface interface (which can be multiple), and the implementation is the most common relationship between classes and interfaces; In Java, such relationships are explicitly identified by the keyword implements

The implementation class diagram is as follows:

Interface Definition Code:
<span style= "FONT-SIZE:14PX;" >package Dim.uml.realization;public interface Interface_a {void dosomething ();} </span>

Class_a Implementation Interface Interface_a
<span style= "FONT-SIZE:14PX;" >package Dim.uml.realization;public class Class_a implements interface_a {@Overridepublic void dosomething () {//TODO Auto-generated Method stub}}</span>


3. Dependence (Dependency)It is also a connection between classes and classes. Represents a class that relies on the definition of another class. Dependencies are always one-way. You can simply use a class A to another Class B, and the use of the relationship is accidental, temporary, very weak, but the change of Class B will affect the A; the programmer wants to play the code, the computer, the programmer and the computer relationship is a dependency, that is, Class A uses another class B; But the computer is poor. Will affect the programmer's productivity, i.e. class B changes will affect Class A. At the code level, for Class B as a parameter, Class A is used in a method.

The class diagram is as follows:

class diagram corresponding code:Programmer class:
<span style= "FONT-SIZE:14PX;" >package Dim.uml.dependency;public class Programmer {computer computer=null;   Public Programmer ()   {   computer=new computer ();   }      public void Coding ()   {computer.runcode (computer);   }} </span>

Computer class:
<span style= "FONT-SIZE:14PX;" >package Dim.uml.dependency;public class Computer {computer () {}public void Recordcode () {System.out.println (" Record code Now "); Public  void Compilecode () {System.out.println ("Compile Code now  "); public void RunCode (computer com) {com.recordcode (); Com.compilecode (); SYSTEM.OUT.PRINTLN ("Run Code Now"); public void Setheadware (String headware) {System.out.println ("set the Headware of Computer");}} </span>

Test class:
<span style= "FONT-SIZE:14PX;"  >package Dim.uml.dependency;public class TestClass {/** * @param args */public static void main (string[] args) {//TODO Auto-generated method Stubprogrammer pr=new Programmer ();p r.coding ();}} </span>

Test results:Print:Record Code Now
Compile Code Now
Run code now

4. Association (association):represents a join between a class and a class that enables a class to know the properties and methods of another class.
Associations can use a single arrow to represent one-way associations, two-way associations with double arrows or no arrows, and two-way associations are not recommended. An association has two endpoints, which can have a cardinality at each endpoint, indicating that the associated class can have several instances.
Common cardinality and Meaning:
A 0..1:0 or 1 instance.
0..*: There is no limit to the number of instances.
1: There can be only one instance.
1..*: There is at least one instance.
He embodies a strong dependency between the two classes, or the semantic level of the class and interface, such as human and human friends, computers and keyboards; this relationship is more than relying on the contingency of dependence, the relationship is not temporary, generally long-term, and the relationship between the two are generally equal, manifested in the code level, To be associated class B in the form of a class attribute in the association Class A, it is also possible that the Association Class A refers to a type is associated with the class B global variables; In the Java language, association relationships are implemented using instance variables.

Human and Friends Class diagram:
(1), Aggregation:A special case of association relationship is a strong association. Aggregation is the relationship between the whole and the individual, that is, the relationship between the whole and the part is has-a, they can have their own life cycle, some can belong to a plurality of whole objects, can also be shared for multiple whole objects, such as computer and CPU, the relationship between the company and employees;performance at the code level, and the association is consistent, can only be differentiated from the semantic level;
The aggregation relationship is also implemented using instance variables. There is no association or aggregation from the Java syntax.
The two classes in an association relationship are at the same level, while the two classes in the aggregation relationship are at unequal levels, one representing the whole, and one representing part. For example: computer and keyboard Relationship:computer and Keyboard class diagram:

Computer class
Package Dim.uml.association;public class Computer {KeyBoard keyboard=null; Computer (String Name) {System.out.println (name+ "  computer"); keyboard=new KeyBoard ();} public void Surfing () {keyboard.type (); System.out.println ("Surfing");}}

Keyboard class
Package Dim.uml.association;public class KeyBoard {public KeyBoard () {//TODO auto-generated constructor stub}public void Type () {System.out.println ("typing");}}

Test class:

Package Dim.uml.association;public class Testassociation {public static void main (string[] args) {computer com=null;com= New Computer ("FAKE"); com.surfing ();}}

(2), combination:Is also a special case of association, he embodies a contains-a relationship, which is stronger than aggregation, also known as strong aggregation, he also embodies the relationship between the whole and part, but at this time the whole and part is not divided, the whole life cycle end also means that part of the life cycle end , like you and your brain; Synthetic relationships cannot be shared. 。 performance at the code level, and the association relationship is consistent, only from the semantic level to differentiate.
The combination is almost identical to the aggregation, the only difference is that the "part" cannot be separated from the "whole", that is, the "part" of the life can not be longer than the "whole".
composition Relationship class diagram:
semantic understanding of composition and aggregation:1, for the desktop keyboard, is a part of the computer, but can be separated. "If it is a notebook that is semantically a combination of relationships, the whole is not part of it. can also be exploited, notebook computer keyboard can also be removed. At that time, it was semantically a party relationship. 】
2, computer and CPU relationship. If it is the semantic understanding of assembling a computer, it is a party relationship, you can assemble the computer with different models of CPU. If the normal user uses the computer, the power use, the computer and the CPU is the combination relationship. The computer and CPU can not be separated.
but aggregation and composition are represented at the code level, and the correlation is consistent and can only be differentiated from the semantic level;
Degree of association: Dependency < Association < aggregation < combination

Resources:

"Startuml Detailed Tutorial"

UML: A diagram of the six major relationships in UML

UML Relationships (generalization, implementation, dependency, association (aggregation, composition))




At present, the level is very little, there is insufficient, a lot of correction, common progress!


The basic concept of UML "dependency, association, generalization, implementation"

Related Article

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.