We have all learned unl, and there are 6 types of relationships in the class diagram: extensive China (inheritance), implementation, association, combination, aggregation, and dependency. As there are various relationships between them, I divide them into four categories: dependency, association, inheritance, and implementation.
Organize
From this figure, we can see how I divide it: Aggregation and combination are a special form of association, So we divide it like this.
We know the Coupling Degree of these six links: inheritance = implement <dependency <Association <aggregation <combination.
Next, let's look at how they are implemented in the design pattern code.
1. Dependency
The definition of dependency is very difficult. To put it bluntly, one object depends on the services of another object. The implementation in C # is very simple: A depends on object B. In most cases, object B is the method parameter, method local variable, or static method call of object.
For example:
The growth of plants requires water and sunlight. Sunlight and water are parameters of plant absorption and absorption methods. C # code implementation:
Public class plant {public void absort (sunshine Sun) {console. write ("absorbing sunlight");} public void shutup (water) {console. write ("moisture absorption ");}}
2. Association
For two relatively independent objects, a fixed ing relationship exists between the instance of one object and the specified instance of another object. Association: Class A uses Class B as the member variable.
For example, the Code implementation is:
Public class son {Father = new father (); Public void getgift () {console. Write ("from" + father. getname () + "get gift ");}}
3. Aggregation
Aggregation is a special association that reflects the relationship between "whole" and "part.
The Coupling Degree of the aggregation relationship is stronger than that of the Association, and their code performance is the same, only in terms of semantics: There is an inclusive relationship between the objects of the aggregation relationship, they are the "Whole-individual" relationship.
Code implementation:
Public class people {car; house; // classes in the aggregate relationship that act as member variables generally use the set method to assign values to public void setcar (car) {This. car = car;} public void sethouse (house) {This. house = house;} public void Driver () {<span style = "font-family: Arial; "> </span> <PRE name =" code "class =" CSHARP "> console. write ("car model:" + car. getType ();} public void sleep () {<PRE name = "code" class = "CSHARP"> console. write ("I am sleeping in the House:" + house. getaddress ());}}
4. Combination
A combination is a special association. The coupling is greater than aggregation, and the code implementation is the same. The difference is that "whole" in a combination cannot be missing "part ".
Code implementation:
<Span style = "font-size: 18px;"> public class body {ear EAR = new ear; eyes = new eyes; Public void listen () {console. write ("body needs" + ear. getname () + "");} public void sleep () {console. write ("required during sleep" + ear. getname () + "close") ;}</span>
5. Inheritance
Inheritance can be divided into two types: Inheriting abstract classes and implementing interfaces. The two differences are:
The abstract class indicates what this object is. The interface indicates what the object can do.
First, the interface is a variant of the abstract class, and all the methods in the interface are abstract. Abstract classes are classes that declare the existence of methods rather than implementing them.
Second, the interface can be inherited more than the abstract class.
Third, the interface definition method cannot be implemented, while the abstract class can implement some methods.
Fourth, the basic data type in the interface is static, but the image is not.
6. Implementation
The Coupling Degree and inheritance of the implementation are the same. The implementation is a class to implement one or more interfaces. Interface Definition methods and class implementation methods.
OK. The Inheritance and implementation code is actually very simple. keywords are used, and the keywords in different languages are different.
Summary:
With regard to the design pattern, if you do not know how the relationship in the UML diagram is implemented, then it is completely overhead. The design pattern requires us not only to understand the UML diagram, but also to apply it flexibly, which is the basis of the design pattern.