1. Class Diagram overview
What is a class diagram?
It describes a diagram that shows a set of classes, interfaces, collaborations, and relationships between them.
What are the relationships between classes and classes?
Four relationships: Dependency, association "with special Aggregation and composition (composition)", Generalization (inheritance), implementation.
2. Dependency relationship
A dependency is a semantic relationship between two classes, and a change to a class (provider) can affect or provide a message to other classes (customers). That is, the customer relies on the provider in some way.
Dependency emphasizes that class changes cause other classes to change accordingly, not only because of the association between classes, but also because of the class's parameter changes (which are also classes) and the message passing mechanism between classes. As long as the class has changed to cause another class to change, it can be said that there is dependency.
such as the TV class and the Channel Class code:
// TV class Public class TV { // Channel class generates dependencies as formal parameters Public void Changechannel (Channel c) {} }
// Channel class Public class Channel {}
Its class diagram relationship design:
3. Related relationships
Association relationships represent an intrinsic relationship between elements and describe the structural relationships between classes.
An association behaves as if one object can obtain an instance reference of another object and invoke its service (that is, use it). Defines a relationship rule between a class or object, which indicates that a class contains an object of another class as an attribute, and it generally behaves as a property of a class (instance variable).
For example, the company class and the Employee Class code:
// Company Class Public class company { // include its object as an attribute to create an association relationship Public Get Set ; } }
// Employee Class Public class Employee {}
Its class diagram relationship design:
4. Aggregation relationship
The aggregation relationship is a special kind of association , the aggregation is the relationship between the whole and the individual, that is, the has-a relationship, when the whole and the part can be separated, they can have their own life cycle, some can belong to many whole objects, can also be shared as multiple whole objects.
Aggregation represents the existence of an instance of an owning or belonging relationship between instances of two classes, a weaker "whole-part" relationship, or a logically "subordinate" relationship.
such as car class and wheel class code:
// Automobile class Public class Car { // include its object as an attribute to produce an aggregation relationship Public Get Set ; } }
// Wheel Class Public class Wheel {}
Its class diagram relationship design:
5. Composite relationships
The composite relationship can not be separated from the whole and part of the aggregation relationship, that is, the life cycle of the partial class is not independent of the whole class, that is, some classes cannot be separated from the whole and exist alone. The whole and the individual are contains-a relations.
such as the head class and the mouth Class code:
// Head class Public class Head { // include its object as an attribute to produce a compound relationship Public Get Set ; } }
// Mouth type Public class Mouth {}
Its class diagram relationship design:
6. Inheritance Relationship
An inheritance relationship indicates a " general and special relationship " between classes. It is only used on the type, not on the instance. In a class, a generic element is called a superclass or a parent class, and a special element is called a subclass.
such as the person class and the man class code:
// Human Public class person { publicintgetset;} }
// male human inherit person class Public class Man:person {}
Its class diagram relationship design:
7. Implementing the Relationship
Implementing relationships is a semantic relationship between classes. A contract is specified between two instances, the entity defines one contract, and the other entity guarantees that the contract is fulfilled. Implementation relationships are typically used between interfaces and classes that implement the interface.
Its Itimeprovider class and Timeprovider class code:
// Time Provider Interface class Public Interface Itimeprovider { DateTime GetDate (); }
// time provider implements Class inheritance Itimeprovider interface Public class Timeprovider:itimeprovider { public DateTime GetDate () { thrownew NotImplementedException (); } }
Its class diagram relationship design:
8. Relationship Strength
The result is strong to weak: inheritance = implementation > Composite relationships > Aggregation Relationships > Association relationships > Dependencies.
Class Diagram relationships