[design pattern from bronze to king] Part III: UML class diagram

designpatternbronzekingiii

 Insert picture description here

List of articles



Preface


 Insert picture description here

One 、UML Class diagram

UML There are many kinds of pictures , But you don't have to master all UML chart , In order to complete the system analysis and design work . As a general rule , stay UML In the figure , Just master the class diagram 、 Use case diagram 、 Use of sequence diagram , You can do most of the work . in other words , master UML Of 20%, Can do 80% Things about . For programmers , The most frequently used is the class diagram . therefore , Here we only talk about UML Class diagram . As for others UML chart , Please refer to more in future work UML Learning materials continue to learn .

Class diagram is the most common and important diagram in object-oriented system modeling , Is the basis of defining other graphs . Class diagram is mainly used to display the classes in the system 、 A static model of interfaces and their static structures and relationships . The most basic element in the class diagram is the class 、 Interface . After the software designer designs the class diagram , Programmers can use code to implement the contents contained in the class diagram .

Two 、 Concrete classes in class diagram 、 abstract 、 Representation of interfaces and packages

UML Concrete classes in class diagram 、 abstract class 、 Interfaces and packages have different representations .

1. stay UML A class diagram represents a concrete class

  • The concrete class is represented by a rectangular box in the class diagram , The rectangle is divided into three layers :
  • ● The first level is the class name .
  • ● The second layer is the member variable of the class .
  • ● The third level is class methods .
  • Member variables and access modifiers before methods are represented by symbols :
  • ● + Express public.
  • ● - Express private.
  • ● # Express protected.
  • ● No sign indicates default.
     Insert picture description here

2. stay UML Abstract classes are represented in class diagrams

Abstract class UML Class diagrams are also represented by rectangular boxes , However, the class name of the abstract class and the name of the abstract method are expressed in italics .
 Insert picture description here

3. stay UML The interface is represented in the class diagram

  • Interfaces are also represented by rectangular boxes in the class diagram , But unlike the representation of classes :
  • ● The top of the first floor is made of structure <> Express , The following is the interface name .
  • ● The second layer is the method .

 Insert picture description here
Besides , There is another representation of interfaces , Commonly known as lollipop , It's a lollipop on the class ( circle + Solid line ). Next to the circle is the interface name , Interface methods appear in the implementation class .

4. stay UML The class diagram represents the package

Classes and interfaces generally appear in packages ,UML The package representation in the class diagram is shown in the following figure :

 Insert picture description here

3、 ... and 、 Represent relationships in class diagrams

Classes and classes 、 Classes and interfaces 、 There is a certain relationship between interfaces ,UML There are usually lines in the class diagram to indicate the relationship between them . There are six types of relationships , They are implementation relations 、 Generalization relation 、 Connections 、 Dependency relationship 、 Aggregate relationship 、 synthetic relation , As shown in the figure below :
 Insert picture description here

1. Realization relationship

Implementation relationship refers to the relationship between interfaces and their implementation classes . stay UML In class diagram , The implementation relationship is represented by an arrow composed of a hollow triangle and a dotted line , Point to the interface from the implementation class , As shown in the figure below . stay Java In the code , The implementation relationship can be directly translated into keywords implements.
 Insert picture description here

2. Generalization relation

Generalization relation (Generalization) It refers to the inheritance relationship between objects . If the object A And the object B Between “is a” Relationship established , Then there is an inheritance relationship between them , object B Is a parent object , object A Is a child object . for example , An annual salary employee “is a” staff , It's obvious that annual salary employees Salary Objects and employees Employee There is an inheritance relationship between objects ,Employee Object is the parent object ,Salary An object is a child object .

stay UML In class diagram , The generalization relation is represented by an arrow composed of a hollow triangle and a solid line , From child to parent , As shown in the figure below . stay Java In the code , The generalized relationship between objects can be directly translated into keywords extends.
 Insert picture description here

3. Connections

Connections (Association) Refers to the connection between objects , It makes one object know the properties and methods of another object . stay Java in , The code expression of association relationship is that one object contains a reference to another object . in other words , If an object's class code , Contains a reference to another object , Then the two objects are related .

There are unidirectional Association and bidirectional association . If both objects know ( You can call ) Public properties and operations of the other party , Then the two are two-way correlation . If only one object knows ( You can call ) Public properties and operations of another object , So it's one-way Association . Most associations are unidirectional , One way relationships are easier to establish and maintain , Help find reusable classes .

stay UML In the figure , The two-way association relationship is represented by a solid line with double arrows or a solid line without arrows . One way association is represented by a solid line with an arrow , The arrow points to the associated object , As shown in the figure below . This is navigation (Navigatity).

  • An object can hold an array or collection of other objects . stay UML in , By placing multiplicity (multipicity) The expression is expressed at the end of the association line . The multiplicity expression can be a number 、 A range or a combination of them . Examples of expressions allowed by multiplicity are as follows :
  • ● Numbers : Exact quantity .
  • ● * perhaps 0…*: Express 0 To more than one .
  • ● 0…1: Express 0 perhaps 1 individual , stay Java An empty reference is often used to implement .
  • ● 1…*: Express 1 To more than one .

Relationships are divided into dependent relationships 、 There are three types of aggregation Association and combination Association .

4. Dependency relationship

rely on (Dependency) Relationship is a weak relationship . If the object A Use object B, But and B When the relationship is not too obvious , You can think of this relationship as a dependency . If the object A Depends on the object B, be A “use a” B. For example, the relationship between driver and car , The driver uses the car , There is a dependency between the two .

stay UML In class diagram , Dependencies are represented by an arrow with a dashed line , From the user to the used party , Indicates that the user object holds a reference to the used object , As shown in the figure below .

 Insert picture description here
Dependency in Java The specific code expression in is B by A A local variable in a constructor or method of 、 Method or constructor parameters 、 Return value of method , perhaps A call B Static method of .

Let's use the code list 1 And code list 2 Shown Java Code to demonstrate the dependencies between objects .

Code list 1 Shown B Class defines a member variable field1, A common method method1() And a static method method2().

// Code list 1 B.java
public class B {

public String field1; // Member variables 
public void method1() {

System.println(" In the class B Methods 1 in ");
}
public static void method2() {
 // Static methods 
System.out.println(" In the class B Static method of 2 in ");
}
}

Code list 2 Shown A Classes depend on B class , stay A Class defines four methods , Demonstrate the four forms of dependency .

/* Code list 2 A.java A Depend on B */
public class A {

public void method1() {

//A Depend on B The first form of expression of :B by A Local variables of 
B b = new B();
b.method1();
}
public void method2() {

//A Depend on B The second form of expression of : call B Static method of 
B.method2();
}
public void method3(B b) {

//A Depend on B The third form of expression of :B As A Method parameters of 
String s = b.field1;
}
//A Depend on B The fourth form of expression of :B As A The return value of the method 
public B method4() {

return new B();
}
}
### 5. Aggregation relation and combination relation polymerization (Aggregation) It's a special case of association , It embodies the ownership relationship between the whole and the part , namely “has a” The relationship between . At this time, the whole and the part are separable , They can have their own life cycles , Parts can belong to more than one whole object , It can also be shared for multiple whole objects , Therefore, aggregation relationship is often called sharing relationship .** for example , The relationship between the company's departments and employees , An employee can belong to multiple departments , A department abolished , Employees can transfer to other departments .**

stay UML In the figure , The aggregation relationship is represented by a hollow diamond and a solid arrow , The hollow diamond is on one side of the whole , The arrow points to one side of the part , As shown in the figure below .
 Insert picture description here
Combine (Composition) It is also a special case of correlation , It also reflects the inclusive relationship between the whole and the part , namely “contains a” The relationship between . But at this time, the whole and the part are inseparable , Part cannot be shared with other whole , The object as a whole is responsible for the life cycle of a part of the object . This relationship is stronger than aggregation , Also called strong polymerization . If A Combine B, be A Need to know B Life cycle of , It's possible A Responsible for generating or releasing B, perhaps A Know in some way B The generation and release of .

for example , People include heads 、 trunk 、 Limbs , Their lifecycles are consistent . When people are born , head 、 trunk 、 The limbs were born at the same time . When people die , As a part of the human body 、 trunk 、 The limbs die at the same time .

stay UML In the figure , The combination relationship is represented by a solid diamond and a solid arrow , The solid diamond is on one side of the whole , The arrow points to one side of the part , As shown in the figure below .
 Insert picture description here
stay Java In code form , A partial object in an aggregation and composition relationship is a member variable of the overall object . however , In practical application development , Whether the relationship between two objects is aggregation or composition , Sometimes it's hard to tell . stay Java in , Aggregation and composition cannot be distinguished only from the class code itself . If you must distinguish , When deleting the whole object , Some objects must be deleted , So it's a combination relationship , Otherwise, it may be an aggregation relationship . From a business perspective , If the object as a whole must involve some objects , To complete their duties , Then there is a combinatorial relationship between the two , Otherwise, it is an aggregation relationship .

for example , Cars and tires , The car as a whole , Tyres as part . If used in the used car sales business environment , There is an aggregation relationship between the two . Because the tire is an integral part of the car , It and cars can be produced separately and then assembled for use , But cars can have new tyres , Tyres can also be unloaded for use by other cars . If used in the driving system business environment , If a car has no tires , You can't complete the driving task , There is a combinatorial relationship between the two . Another example is the relationship between orders and order items in the online bookstore business , If the order has no order items , It will be impossible to complete the order business , So there is a combinatorial relationship between the two . And the relationship between shopping cart and goods , Because the product life cycle is not controlled by the shopping cart , Items can be shared by multiple shopping carts , therefore , There is an aggregate relationship between the two .

Four 、 Example

 Insert picture description here

  • ● The class diagram structure of the car is <>, Indicates that the car is an abstract class
  • ● The car contains two implementation classes : Cars and bicycles , Use a dotted line with a hollow arrow to indicate
  • ● Cars and SUV The relationship between them is generalized , Denote by a solid line with a hollow arrow
  • ● Cars and tyres ( Or engine ) It's a combination relationship , Denote by a solid line with a filled arrow
  • ● Students and classes are converging , Use a solid line with a hollow diamond to represent
  • ● There is an association between students and ID cards , Use a solid line to show
  • ● Students need bicycles to go to school , It's a dependency relationship with bicycles , Use dashed lines with arrows to indicate

summary

That's what we're going to talk about today , This paper introduces in detail the design pattern UML Class diagram introduction and use , The following articles will explain the use of various design patterns in detail , Please pay more attention to , Design patterns provide a number of methods for us to use , It's very convenient , We must master . I hope you can support me more ! In addition, if there are any problems with the above , Please understand my advice , But that's okay , The main thing is that you can stick to , I hope some students who study together can help me correct , But please tell me gently if you can , Love and peace are the eternal theme , Love you . Come on !

 Insert picture description here

copyright:author[The senming sect is bigger than the black tiger sect],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240337433674.html