Writing in the front
- Take notes on learning design patterns
- Improve the flexible use of design patterns
Learning to address
www.bilibili.com/video/BV1G4…
www.bilibili.com/video/BV1Np…
Refer to the article
C.biancheng.net/view/1317.h…
Program source codeGitee.com/zhuang-kang…
2, the UML diagrams
Unified Modeling Language (UML) is a visual Modeling Language for designing software. Its characteristics are simple, unified, graphical, can express the dynamic and static information in software design.
UML defines nine diagrams including use case diagram, class diagram, object diagram, state diagram, activity diagram, sequence diagram, collaboration diagram, component diagram and deployment diagram from different points of view of target system.
2.1 Overview of class diagrams
The Class diagram shows the static structure of the model, especially the existing classes in the model, the internal structure of the classes and their relationship with other classes. Class diagrams do not display transient information. Class diagrams are an important part of object-oriented modeling.
2.2 Functions of class diagrams
- In software engineering, class diagram is a kind of static structure diagram, which describes the collection of classes of the system, the properties of classes and the relations between classes, which can simplify people’s understanding of the system.
- Class diagram is an important product of system analysis and design stage and an important model of system coding and testing.
2.3 Class diagram representation
2.3.1 Representation of classes
In UML class diagrams, classes are represented by delimited rectangles that contain the class name, attribute (field), and method (method). For example, the following figure shows an Employee class that contains the name,age, and Address attributes, as well as the work() method.
The plus and minus signs before a property/method name indicate the visibility of the property/method, and there are three notations for visibility in UML class diagrams:
-
+ : indicates public
-
– : indicates private
-
#:表示protected
The full representation of an attribute is: Visibility Name: type [= default]
The full representation of the method is: visibility name (argument list) [: return type]
Note:
1. The content in parentheses indicates that it is optional
2, also put the type before the variable name and the return value type before the method name
Here’s an example:
The Demo class above defines three methods:
- Method () : the modifier is public, with no arguments and no return value.
- Method1 () : modifiers are private, have no arguments, and return values of type String.
- Method2 () : modifiers protected, takes two arguments, the first of type int, the second of type String, and the return value of type int.
2.3.2 Representation of relationships between classes
2.3.2.1 Association Relationship
An association relationship is a reference relationship between objects. It is used to represent the relationship between one type of object and another type of object, such as teacher and student, teacher and apprentice, husband and wife, etc. Association relation is the most commonly used relation between classes, which can be divided into general association relation, aggregate relation and combinatorial relation. Let’s start with general associations.
Association can be divided into one-way association, bidirectional association and self-association.
1. One-way association
One-way associations are represented by a solid line with an arrow in a UML class diagram. The figure above shows that each Customer has an Address, which is achieved by having the Customer class hold a member variable class of type Address.
2. Bidirectional association
It is easy to see from the figure above that a bidirectional association is one in which each party holds a member variable of the other’s type.
In UML class diagrams, bidirectional associations are represented by a straight line without arrows. In the figure above, a List<Product> is maintained in the Customer class, indicating that a Customer can purchase multiple items. A member variable of type Customer is maintained in the Product class to indicate which Customer purchased the Product.
3. Self-association
Autocorrelation is represented in a UML class diagram as a line with an arrow pointing at itself. The Node class contains member variables of type Node.
2.3.2.2 Aggregation relationship
Aggregation relation is a kind of correlation relation, which is a strong correlation relation and the relation between whole and part.
Aggregation relationships are also implemented through member objects, which are part of the overall object, but can exist independently of the overall object. For example, the relationship between the school and the teacher, the school includes the teacher, but if the school closes, the teacher still exists.
In UML class diagrams, aggregation relationships can be represented by solid lines with hollow diamonds that point to the whole. The diagram below shows the relationship between universities and faculty:
2.3.2.3 Combination relation
Composition represents a whole-part relationship between classes, but it is a stronger aggregation relationship.
In combinatorial relation, the whole object can control the life cycle of some objects. Once the whole object does not exist, some objects will not exist, and some objects cannot exist without the whole object. For example, the relationship between the head and the mouth, without the head, the mouth would not exist.
In UML class diagrams, composition relationships are represented by solid lines with solid diamonds that point to the whole. Here is a head and mouth diagram:
2.3.2.4 Dependency Relationships
Dependency is a kind of use relation, it is the weakest coupling degree between objects of a kind of association, is a temporary association. In code, a method of one class performs some responsibility by accessing some method in another (dependent class) through local variables, method arguments, or calls to static methods.
In UML class diagrams, dependencies are represented by dotted lines with arrows that point from the using class to the dependent class. The diagram below shows the relationship between the driver and the car. The driver drives the car:
2.3.2.5 Inheritance Relationship
Inheritance relation is a relation with the largest degree of coupling between objects. It represents the relation between general and special. It is the relation between parent class and subclass, and it is an inheritance relation.
In UML class diagrams, generalization relationships are represented by solid lines with hollow triangular arrows that point from subclasses to parent classes. When the code is implemented, the object-oriented inheritance mechanism is used to realize the generalization relationship. For example, the Student and Teacher classes are subclasses of the Person class, and the class diagram looks like this:
2.3.2.6 Implementing relationships
An implementation relationship is a relationship between an interface and an implementation class. In this relationship, the class implements the interface, and the operations in the class implement all the abstract operations declared in the interface.
In UML class diagrams, implementation relationships are represented by dashed lines with hollow triangular arrows that point from the implementation class to the interface. For example, cars and boats implement vehicles, and their class diagram is shown in Figure 9.
Write in the last
- If my article is useful to you, please give me a click 👍, thank you 😊!
- If you have any questions, please point them out in the comments section! 💪