This is the 16th day of my participation in the August More Text Challenge.

Dependency, association, aggregation, composition

Class and class diagram

  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 a certain responsibility, responsibility refers to the task of the class, that is, what kind of function to complete the class, what kind of obligations to 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

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;

Ii. 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#.

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.

Iii. 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 either side of the correlation indicate the relationship between the two, while the number indicates the limitation of the relationship and the multiplicity of the relationship between the two. There are usually “” (all, no limit),” 1 “(yes and only one),” 0…” (indicates zero or more), 0, 1 (indicates zero or one), n… M “(n to m),” M…” (Indicates at least m.)

• 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, the association is 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
  1. Self-association: There may be some classes in the system whose attribute object type is the class itself. This special association is called self-association.

public class Node  
{  
    privateNode subNode; ... }Copy the code
  1. Multiplicity association: Multiplicity association, also known as Multiplicity, represents the number of connections between objects of one class and objects 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

The telephone consists of a microphone

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 computer;Copy the code

V. 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

For example, the company and the department are parts, while the company is the whole. The financial department of Company A cannot exchange with the financial department of company B, that is to say, company A cannot be separated from its own financial department. The heart of man.

Vi. 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