Design Pattern-Comparison Between UML relation and code
When learning UML, we should focus on the nine types of diagrams and six relations of UML and Their Derived works. After completing the charging system of the IDC, I learned the theoretical knowledge of UML and practiced it. After modeling the system with UML, we need to convert the UML diagram into specific code and use the code to implement our model. The representation of each type of link in the Code is similar. After reading the code, we can find the relationship between the class and the class.
Inheritance:A class (called a subclass or sub-interface) inherits the functions of another class (called parent class or parent interface) and can add its own new functions.
Code
Class Bird: Animal {// member ;}
Implementation:It refers to the function that a class implements interface interfaces (multiple interfaces can be used.
Interface Representation
1. Draw a lollipop shape directly.
2. Set the form of a class to Interface and select this interface, Format-> Stereotype display-> Lable
Code
Class Bird: IFlay, IWalk // multi-inheritance of interfaces ;{}
Association(Association): associate a class when it knows another class.
Code <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> class Penguin: Bird {private Climate climate; // Penguin inherits birds and references the Climate object}
Dependency(Dependency): If Class A uses Class B, changes to Class B will affect Class.
Code
Abstract class Animal {public Metabolism (Oxygen oxygen, Water water) {// The Water and Oxygen classes serve as parameters or return values of a method of the Animal class ;}}
Combination(Composition): a strong "ownership" relationship, reflecting a strict and integral relationship, the same as the overall life cycle.
Code
Class Bird {private Wing wing; public Bird () {wing = new Wing (); // constructor, instantiate Wing when initializing birds; same lifecycle }}
Aggregation(Aggregation): A weak "having" relationship indicates that object A can contain object B, but object B is not part of object. B can exist independently from. Different lifecycles
Code
Class WideGooseAggregate {private WideGoose [] arrayWideGoose; // The geese in the wild goose group ;}
Every design pattern is given by UML diagrams. The UML diagrams of these patterns are various combinations of class-to-class relationships. They are familiar with the basics and are more conducive to the subsequent understanding of the Code. When you see the code and think of the corresponding UML diagram, you can master the key of the UML diagram of each mode. from an early stage, you can break through each other.