UML Class Diagram Java code implementation

Source: Internet
Author: User
Tags modifiers

Class diagrams are the most common UML diagrams used to describe the structural design of a system. These include class relationships and the properties and behaviors associated with each class. Class diagrams can represent an excellent representation of inheritance and composition relationships. To use the class diagram as an efficient communication tool, the developer must understand how to convert the elements appearing on the class diagram into Java. Let's explore this conversion process further

Elements

In the following subsections, each element of the class diagram and its corresponding representation in Java are explained. I'll list the element names, followed by a short snippet of code and a graph to show how the elements look on the class diagram. Each section concludes with a brief summary of the element

Classes (Class)

The class ( figure A) is the blueprint for the object, which contains 3 components. The first one is the class name defined in Java. The second is a property (attributes). The third is the method that the class provides, properties, and operations that can be appended with a visibility modifier. A plus sign (+) indicates a public visibility. A minus sign (-) indicates private visibility. #号表示受保护的可见性. Omitting these modifiers indicates visibility with the package level. If the property or operation has an underscore, it is static. In the operation, you can list the parameters it accepts as well as the return type, as shown in the "Java" area of a

Figure A

Packages (Package)

Package (Figure B) is a general-purpose combination mechanism. A package in UML directly corresponds to a package in Java. In Java, a package may contain other packages, classes, or both. When modeling, you typically have logical packages that are primarily used to organize your models. You'll also have a physical package, which is converted directly to the Java package in the system. The name of each package uniquely identifies the package

Figure B

Interface (Interface)

An interface ( Figure C) is a collection of operations that specifies the services provided by a class. It corresponds directly to an interface type in Java. The interface can be represented either by the icon in Figure C, or by a standard class with <<interface>> attached. Typically, you can know the relationship to other classes based on what the interface looks like on the class diagram

Figure C

Relationship

The following example will show the relationships independently for a specific purpose. Although grammatically correct, these examples can be further refined to include more semantics within their effective range

Dependency (Dependency)

A "use" relationship between entities implies that the specification of an entity changes and may affect other instances that depend on it ( figure D). More specifically, it can be converted to any type of reference to a class or object that is not within the scope of the instance. This includes a local variable, a reference to an object obtained through a method invocation (as shown in the following example), or a reference to a static method of a class (without an instance of that class). You can also use dependencies to represent the relationship between packages and packages. Because the package contains classes, you can represent the relationship between the package and the package based on the relationship between the classes in those packages

Figure D

Association (Association)

A structured relationship between entities indicates that objects are interconnected. The arrows are optional and are used to specify navigation capabilities. If there are no arrows, hinting is a two-way navigation capability. In Java, the Association ( Figure E) is converted into a variable of an instance scope, just as the code shown in the Java area of Figure E. Additional modifiers can be attached to an association. The multiplicity (multiplicity) modifier implies a relationship between instances. In the model code, an employee can have 0 or more timecard objects. However, each timecard only belongs to a single employee

Figure E

Aggregation (Aggregation)

Aggregation ( figure F) is a form of association that represents a global/local relationship between two classes. Aggregation implies that the whole is conceptually at a higher level than the local, and the association implies that two classes are conceptually at the same level. Aggregations are also converted to an instance-scoped variable in java.
The difference between association and aggregation is purely conceptual, and is strictly reflected in semantics. Aggregation also implies that there are no loops in the instance diagram. In other words, it can only be a one-way relationship

Figure F

Synthesis (composition)

Synthesis ( Figure G) is a special form of aggregation that implies a "partial" lifetime responsibility within the "whole". The composition is also unshared. Therefore, although the local does not have to be destroyed with the overall destruction, but the overall responsibility to maintain a local survival, or be responsible for destroying it. Local cannot be shared with other whole. However, the whole can transfer ownership to another object, which will then assume the lifetime responsibility. The relationship between employee and timecard may be better represented as "compositing" rather than "associative"

Figure G

Generalization (generaliation)

Generalization ( figure H) represents the relationship between a more generalized element and a more specific element. Generalization is a UML element that is used to model inheritance. In Java, this relationship is directly represented by the extends keyword.

Figure h

Implementation (Realization)

The example ( figure I) relationship specifies a contract between two entities. In other words, one entity defines one contract and the other guarantees the performance of the contract. When modeling a Java application, the implementation relationship can be expressed directly by using the implements keyword

Figure I

Recommended Reading

A detailed description of the relationship between aggregation, association and generalization in UML

UML Class Diagram Java code 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.