(Using IDEA, some Eclipse screenshots)

Introduction to UML

  1. UML (Unified Modeling Language) is a language tool for software system analysis and design. It helps software developers think and document the results of their ideas
  2. UML itself is a set of notations, like mathematical and chemical notations, used to describe various elements in a software model and their relationships, such as classes, interfaces, implementations, generalizations, dependencies, compositions, aggregations, and so on
  3. Modeling is done using UML, a common tool is Rational Rose, and plug-ins are also available

(Picture taken from video of station B)

UML diagrams

Drawing UML diagrams is similar to writing articles, both of which are to describe their own ideas to others. The key lies in ideas and order.

  1. Use Case Diagram
  2. Static structure diagram: class diagram, object diagram, package diagram, component diagram, deployment diagram
  3. Dynamic behavior diagram: interaction diagram (sequence diagram and collaboration diagram), state diagram, activity diagram

Description:

  1. Class diagrams describe the relationships between classes and are the core of UML diagrams
  2. When explaining design patterns, we will inevitably use class diagrams

UML class diagrams

  1. Used to describe the composition of classes (objects) themselves and the various static relationships between classes (objects) in the system.
  2. Relationships between classes: dependencies, generalization (inheritance), implementation, association, aggregation, and composition
  3. Simple example of class diagram
public class Person{ // Code form -> class diagram
	private Integer id;
	private String name;
	public void setName(String name){
		this.name=name;
	}
	public String getName(a){
		returnname; }}Copy the code

Class Diagram — Dependence

As long as the other is used in the class, there is a dependency between them. Without the other side, we can’t even get through the stream.

public class PersonServiceBean {

    // Class member attributes
    private PersonDao personDao;// class // is also a type of aggregation

    // The type of argument received by the method
    public void save(Person person) {}// Return type of the method
    public IDCard getIDCard(Integer personid) {
        return null;
    }

    // In violation of Demeter's rule, the Department class appears in a local variable, not a direct friend
    public void modify(a) {
        // is used in the
        // new instantiates the Department object
        Department department = newDepartment(); }}Copy the code

The class diagram

Class –> The class used

summary

Class uses each other

  1. Class member attributes
  2. Method return type
  3. The type of argument received by the
  4. Method

Class graph — Generalization relation

Generalization relation is actually inheritance relation, which is a special case of dependence relation

/ / parent class
public abstract class DaoSupport{
	public void save(Object entity){}public void delete(Object id){}}/ / subclass
public class PersonServiceBean extends DaoSupport {}Copy the code

The class diagram

Subclass –> parent class

Summary:

  1. The generalization relation is actually the inheritance relation
  2. If class A inherits class B, we say that A and B have A generalization relationship

Class Diagram – Implementation Relationships

An implementation relationship is actually A class A implementation of the INTERFACE B, which is A special case of dependencies

    / / interface
    public interface PersonService {
    	public void delete(Integer id);
    }
    
    / / implementation class
    public class PersonServiceBean implements PersonService{
    	@Override
    	public void delete(Integer id) {
    		// TODO Auto-generated method stub
    		System.out.println("delete.."); }}Copy the code

The class diagram

Implementation class –> interface

Summary:

  1. Implementing relationships is really just implementing relationships
  2. If class A implements class B, we say that there is an implementation relationship between A and B

Class Diagram – Association

Relationships are actually relationships between classes, which is a special case of dependencies

Associations are navigational: that is, bidirectional or unidirectional relationships

Relationships have multiplicity: for example, “1” (one and only), “0…” (indicates zero or more), 0, 1 (indicates zero or one), n… M “, “M… * “(indicates at least m numbers).

Unidirectional one-to-one relationship

    // This code, one-way aggregation
    public class Person {
        private IDCard card;
    }
    public class IDCard{}
Copy the code

(Eclipse screenshot on the left, IDEA screenshot on the right)

Bidirectional one-to-one relationship

    // This code, two-way aggregation
    public class Person {
        private IDCard card;
    }
    public class IDCard{
        private Person person
    }
Copy the code

(Eclipse screenshot on the left, IDEA screenshot on the right)

Class Diagram — Aggregation

Aggregation refers to the relationship between the whole and the parts. The whole and the parts can be separated. (Can not be separated is a combination of relations)

Aggregation relation is a special case of association relation, so it has the navigability and multiplicity of association.

For example, a computer is composed of keyboard, monitor, mouse, etc. The components that make up the computer can be separated from the computer and are represented by solid lines with hollow diamonds:

    public class Computer {
        private Mouse mouse; // Mouse can be separated from computer
        private Moniter moniter;// Display can be separated from Computer
        
        // Dashed lines, method parameters, dependencies
        public void setMouse(Mouse mouse) {
            this.mouse = mouse;
        }
        
        // Dashed lines, method parameters, dependencies
        public void setMoniter(Moniter moniter) {
            this.moniter = moniter; }}Copy the code

The class diagram

Arrows point to the referenced object class and diamonds point to the referenced class

Class Diagram — Composition

Combinatorial relation: also the relation of whole and part, but whole and part cannot be separated.

Let’s look at another example: in the program we define entities: Person, IDCard, Head. Then Head and Person are combinations, and IDCard and Person are aggregates. However, if the cascade deletion of IDCard is defined in the Person entity in the program, that is, the IDCard is deleted together with the Person when the Person is deleted, then the IDCard and Person are combined.

    public class Person {
        private IDCard card; // Aggregate relationship
        private Head head = new Head(); // Combine
        
        // Dashed lines, method parameters, dependencies
        public void setCard(IDCard card) {
            this.card = card;
        }
        // Dashed lines, method parameters, dependencies
        public void setHead(Head head) {
            this.head = head; }}Copy the code

The class diagram

conclusion

dependencies

  • 1. Dotted arrow
  • 2. Member attributes (aggregation and composition), method parameters, return types, and methods used in
  • 3. This class –> used class

Generalization relationship

(Special cases of dependencies)

  • 1. Solid arrow
  • 2. Inheritance
  • 3. Subclass –> Parent class

Realize the relationship between

(Special cases of dependencies)

  • 1. Dotted triangle arrow
  • 2. Implement relationships
  • 3. Implementation class –> interface

correlation

(Special cases of dependencies)

  • 1. Two-way or one-way relationship
  • 2. Look specifically at the aggregation relationship and combinatorial relationship

The aggregation relationship

(Special cases of relational relationships)

  • 1. Solid line, head: arrow, tail: diamond
  • 2. Divisible, object member variable (one case, non-new object)
  • 3. The arrow points to the referenced object class, and the diamond points to the referenced class

Combination relationship

(Special cases of relational relationships)

  • 1. Dotted arrow (with create)
  • 2. Indivisible, new object (one case)
  • 3. Class –> New object class