preface

1. Class encapsulates data and behavior and is an important part of object orientation. It is the general name of objects with the same attributes, operations and relations. 2. In the system, each class has certain responsibilities. Responsibilities refer to the tasks performed by the class, that is, what functions and obligations the class should undertake. A class can have multiple responsibilities. A well-designed class usually has only one responsibility. When defining a class, the responsibilities of the class are broken down into properties and operations (methods) of the class. 3. The attributes of a class are the data responsibilities of a class, and the operations of a class are the behavioral responsibilities of a class

In UML class diagrams, the following relationships are common:Generalization (Generalization),Implementation (Realization.Connection (Association).Aggregation.Composition, Dependency

1. Dependence

Dependence: It is said that class B depends on class A if the change of class A causes the change of class B.

• A Dependency is a usage relationship in which changes to a particular thing may affect other things that use it, and dependencies are used when there is a need to indicate that one thing uses another. In most cases, dependencies occur when a method of one class takes an object of another class as an argument.

• In UML, dependencies are represented by dashed lines with arrows, pointing from the dependent to the dependent.

public class Driver
{
    public void drive(Car car)
    {
        car.move();
    }
    ……
}
public class Car
{
    public void move(a)
    {... }... }Copy the code

There are three types of dependencies:

1. Class A is A local variable of class B.

Class A is A parameter in A class B method.

3. Class A sends messages to class B, thus affecting the change of class B;

2. Generalization relations

Generalization: A is the Generalization of B and C, and B and C have the common class (parent class) A, which means that A is Generalization of B and C.

• Generalization relations are Generalization relations, also called “IS-A-kind-of” relations, which describe the relationship between the parent class, which is called a base class or superclass, and the subclass, which is called a derived class. In UML, generic relationships are represented by straight lines with hollow triangles.

• when code is implemented, use object-oriented inheritance mechanisms to implement generalization relationships, such as the extends keyword in the Java language and the colon ‘:’ in C++/C#.

Note: “+” indicatespublic, “#” meansprotected, “-” indicatesprivate
public class Person 
{
    protected String name;
    protected int age;
    public void move(a) 
    {
        ……
    }
    public void say(a) 
   {... }}public class Student extends Person 
{
    private String studentNo;
    public void study(a) 
    {... }}Copy the code

In UML, there are three requirements for generalization relationships:

1, subclass and parent class should be exactly the same, the parent class has attributes, operations, subclass should have;

2. A subclass contains additional information in addition to the information consistent with the parent class;

3. Where you can use instances of a parent class, you can also use instances of a child class.

3. Association

Association: Associations between classes, such as customers and orders, with each order corresponding to a specific customer, each customer corresponding to a specific number of orders, and the Association between basketball players and teams (as shown in the figure below).

The “Employee” and “employer” on both sides of the correlation indicate the relationship between the two, and the number indicates the limitation of the relationship, the multiplicity of the relationship between the two. There are usually “*” (all, no limit), “1” (one and only one), “0…” (indicates zero or more), 0, 1 (indicates zero or one), n… M “, “M… * “(indicates at least m numbers).

• Association is the most common type of relationship between classes. It is a structural relationship used to indicate that one class of objects is related to another.

• in UML class diagrams, solid lines are used to connect the corresponding classes of the associated objects. When implementing associations in programming languages such as Java, C#, and C++, objects of one class are often treated as properties of another class.

• Role names can be annotated on the association line when class diagrams are used to represent associations.

1) Bidirectional association: By default, associations are bidirectional.

public class Customer
{
    privateProduct[] products; ... }public class Product
{
    privateCustomer customer; ... }Copy the code

2) One-way association: The association relationship of a class can also be one-way, and one-way association is represented by a solid line with an arrow.

public class Customer
{
    privateAddress address; ... }public class Address
{
    ……
}
Copy the code

3) Self-association: There may be some classes whose attribute object type is the class itself in the system, and this special association relationship is called self-association.

public class Node
{
    privateNode nextNode; ... }Copy the code

Multiplicity relationship: Multiplicity relationship is also called Multiplicity. It represents the number of connections between an object of one class and an object of another class. Multiple relationships in UML can add a number directly to an association line to represent the number of objects of another class corresponding to it.

representation Multiplicity specification
1.. 1 An object representing another class is related to only one object of that class
0.. * An object representing another class is related to zero or more objects of that class
1.. * An object representing another class is related to one or more objects of that class
0.. 1 An object representing another class has no or only a relationship with an object of that class
m.. n An object representing another class is related to at least m and at most N objects of that class (m<=n)
public class Form
{
    privateButton buttons[]; ... }public class Button
{...Copy the code

4. Aggregation

Aggregation refers to the relationship between the whole and the parts. The whole and the parts can be separated.

• Aggregation refers to the relationship between a whole and its parts. Usually, after defining a whole class, the composition structure of the whole class is analyzed to find out some member classes, and an aggregation relationship is formed between the whole class and the member classes.

• In an aggregation relationship, a member class is part of the whole class, that is, the member object is part of the whole object, but the member object can exist independently of the whole object. In UML, aggregation relationships are represented by straight lines with hollow diamonds.

public class Car
{
    private Engine engine;
    public Car(Engine engine)
   {
        this.engine = engine;
    }
    
    public void setEngine(Engine engine)
    {
        this.engine = engine;
    }
    ……
}
public class Engine
{
    ……
}
Copy the code

Such as: computer includes keyboard, monitor, a computer can and multiple keyboards, multiple monitors collocation, determine the keyboard and monitor can be separated from the host, the host can choose other keyboard, monitor composed of computer;

5. Composition

Composition: Also the relation between the whole and the parts, but the whole and the parts cannot be separated.

• Composition also refers to the whole and part relationships between classes, but the parts and the whole have a uniform lifetime. Once the whole object does not exist, part of the object will not exist, part of the object and the whole object have the relationship of life and death.

• In a combinatorial relationship, a member class is part of the whole class, and the whole class can control the life cycle of the member class, that is, the existence of the member class depends on the whole class. In UML, composition relationships are represented by straight lines with solid diamonds.

public class Head
{
    private Mouth mouth;
    public Head(a)
    {
    mouth = new Mouth();
    }
    ……
}
 
public class Mouth
{
    ……
}
Copy the code

6. Implementation

Implementation relationships: Relationships between classes or building structures used to specify interfaces and solid-line interfaces. Interfaces are collections of operations that are used to specify classes or build a service.

• There can be inheritance and dependency relationships between interfaces that are similar to relationships between classes, but there is also a Realization relationship between an interface and a class in which the class implements the interface and the operations in the class implement the operations declared in the interface. In UML, the implementation relationships between classes and interfaces are represented by dashed lines with hollow triangles.

public interface Vehicle 
{
    public void move(a);
}
public class Ship implements Vehicle
{
    public void move(a) 
    {... }}public class Car implements Vehicle
{
    public void move(a) 
    {... }}Copy the code