Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of diagrams such as Java Design pattern UML

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the Java design pattern UML diagram example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Basic introduction to 1.UML

UML--Unified modeling language UML (Unified Modeling language) is a language tool for software system analysis and design. It is used to help software developers to think and record the results of ideas.

UML itself is a set of symbolic rules, just like mathematical symbols and chemical symbols. These symbols are used to describe the various elements in the software model and their relationships, such as classes, interfaces, implementation, generalization, dependency, composition, aggregation, and so on, as shown in the following figure:

The screenshot above is an example of a UML class diagram, but this is in eclipse, and I will use the following class diagram in IDEA.

Drawing UML diagrams is similar to writing articles. They all describe their own thoughts to others. The key lies in their ideas and organization. UML diagram classification:?

Use case diagram (use case)

Static structure diagram: class diagram, object diagram, package diagram, component diagram, deployment diagram

Dynamic behavior diagrams: interaction diagrams (sequence diagrams and collaboration diagrams), state diagrams, activity diagrams

Class diagram, which describes the relationship between classes, is the core of UML diagram.

2.UML class diagram

Used to describe the composition of the class (object) itself and the various static relationships between the classes (objects) in the system.

Relationships between classes: dependency, generalization (inheritance), implementation, association, aggregation, and composition.

2.1 Class relationships-dependencies

As long as each other is used in a class, there is a dependency between them. Take a look at the following code

Package com.szh.uml.dependence; public class PersonServiceBean {private PersonDao personDao; public void save (Person person) {} public IDCard getIDCard (Integer personId) {return null;} public void modify () {Department department = new Department ();}} package com.szh.uml.dependence; public class PersonDao {} package com.szh.uml.dependence Public class Person {} package com.szh.uml.dependence; public class IDCard {} package com.szh.uml.dependence; public class Department {}

In the above code, Department, Person and other classes are used in the PersonServiceBean class, so we can say that there is a dependency between the PersonServiceBean class and several of their classes.

Each other is used in the class

Member properties of the class

The return type of the method

The type of parameter received by the method

The internal use of the method body

The dependent symbol is a dashed line + an arrow.

2.2 Class relationships-generalization

Generalization is actually inheritance, which is a special case of dependency.

If class An inherits class B, then we can say that there is a generalization relationship between An and B.

Package com.szh.uml.generalization; public abstract class DaoSupport {public void save (Object entity) {} public void delete (Object id) {}} package com.szh.uml.generalization; public class PersonServiceBean extends DaoSupport {}

There is no more about inheritance. One thing to point out here is that the generalization symbol in IDEA is different. Generally speaking, the generalization symbol should be a solid line + a hollow triangle, while the following figure is shown in IDEA.

2.3 Class relationships-implementation

The implementation relationship is actually interface-oriented, and like generalization, it is also a special case of dependency relations.

If class An implements interface B, then we can say that there is an implementation relationship between An and B.

Package com.szh.uml.implementation; public interface PersonService {public void delete (Integer id);} package com.szh.uml.implementation; public class PersonServiceBean implements PersonService {@ Override public void delete (Integer id) {System.out.println ("delete..");}}

I won't say much about implementing the interface. One point to point out here is that the implementation symbol in IDEA is different. Generally speaking, the implementation symbol should be a dotted line + a hollow triangle, while the following figure is shown in IDEA.

2.4 Class relationships-associations

The association relationship is actually the relationship between classes, which is a special case of dependency relations. The association is navigational, that is, two-way relationship or one-way relationship.

Relationships have multiplicity: such as "1" (meaning there is and only one), "0." (for 0 or more), "0focus 1" (for 0 or one), "n... .m" (for either n to m), "m. *" (for at least m)

Package com.szh.uml.association1; / * * * / public class Person {private IDCard card;} package com.szh.uml.association1; / * * * / public class IDCard {}

Here our understanding is that the Person class represents the person, the IDCard class represents the ID card class, a person has only one ID number, and an ID number belongs to only one person, so there is an one-to-one relationship between them. The associated symbol is a solid line + an arrow.

The following figure shows an one-way association, which is converted to two-way when we declare the member variables of the Person class in the IDCard class. (there are also combination symbols in the picture, which we will talk about later.)

2.5 Class relationships-aggregation

Aggregation represents the relationship between the whole and the part, which can be separated from the whole. Aggregation relation is a special case of association relation, so it has the navigation and multiplicity of association.

For example, a computer consists of a keyboard (keyboard), a monitor (monitor) and a mouse; the accessories that make up the computer can be separated from the computer and are represented by a solid line with a hollow diamond.

Package com.szh.uml.aggregation; public class Computer {private Mouse mouse; private Monitor monitor; public void setMouse (Mouse mouse) {this.mouse = mouse;} public void setMonitor (Monitor monitor) {this.monitor = monitor;}} package com.szh.uml.aggregation; public class Monitor {} package com.szh.uml.aggregation; public class Mouse {}

Generally speaking, the symbol of aggregation should be solid line + hollow diamond, and the symbol of combination should be solid line + solid diamond. The symbols of aggregation and combination in IDEA are all like this.

2.6 types of relationships-combinations

Combinatorial relationship: it is also the relationship between the whole and the part, but the whole and the part can not be separated.

Look at another example: in the program, we define entities: Person and IDCard, Head, then Head and Person are combinations, and IDCard and Person are aggregations.

But if cascading deletion of IDCard is defined in the Person entity in the program, that is, when deleting Person, it is deleted along with IDCard, then IDCard and Person are combined.

Package com.szh.uml.composition; public class Computer {private Mouse mouse = new Mouse (); private Monitor monitor = new Monitor (); public void setMouse (Mouse mouse) {this.mouse = mouse;} public void setMonitor (Monitor monitor) {this.monitor = monitor;}} package com.szh.uml.composition; public class Monitor {} package com.szh.uml.composition Public class Mouse {} package com.szh.uml.composition; public class Person {private IDCard card; private Head head = new Head ();} package com.szh.uml.composition; public class Head {} package com.szh.uml.composition; public class IDCard {}

The above is all the content of the article "sample Analysis of diagrams such as Java Design pattern UML". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report