This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Six relationships between classes
- Generalization –> The table inheritance relationship
- Realization
- Association
- Aggregation
- Compostion
- Dependency
Preface:
Recently, the unified modeling language UML is used in the graduation design of the school. I want to make a small record.
I hope this article can bring you some harvest, let you take advantage of the fun.
Class diagram of a single class
Step by step, let’s learn how to use UML diagrams to represent individual classes.
I first put the class paste below:
package uml;
/ * * *@Author: crush
* @Date: 2021-09-30 15:00
* version 1.0
*/
public class Person {
private String name;
private Integer age;
private static String school="A primary school";
public static String nationality="China";
public Person(a) {}public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public static String getSchool(a) {
return school;
}
public static void setSchool(String school) {
Person.school = school;
}
public static String getNationality(a) {
return nationality;
}
public static void setNationality(String nationality) {
Person.nationality = nationality;
}
public void selfIntroduction(String name, Integer age, String school){
System.out.println("May I introduce myself? My name is:"+name+This year, ","+age+"I'm old and I'm from."+school); }}Copy the code
This class is still very simple, so how do you describe it in a class diagram?
The diagram below:
Explanation:
-
The top half is the Person class properties, and the bottom half is the Person class methods
-
– name:String Describes: private String name;
The – sign indicates that the property is private, and the reverse sign indicates that the property is public
Name: indicates the attribute name
: XXX: indicates the type of the attribute. The value is String
-
-school :String=” School “: private static String School =” School “;
The underscore indicates that this property is static.
“Kindergarten” indicates a default value. Ditto for others.
-
+ getNationality():String
public static void setNationality(String nationality) { Person.nationality = nationality; } Copy the code
As above, + means public, underline means static, getNationality() means method name, and String means return value of type String.
But in ordinary times, we are usually multiple classes of relationships, not a lonely old man.
Relationships between classes
There are six ways to express relationships between classes:
- Generalization –> States inheritance relations (the solid line of triangular arrows, which point to the parent class)
- Realization (dashed line of triangular arrow pointing to interface)
- Association (solid line of ordinary arrow pointing to owner)
- A solid line of hollow lozenge that points to the whole
- Compostion (solid line with solid diamonds pointing towards the whole)
- Dependency (dotted line of arrow pointing to used)
Class diagrams for inheritance and implementation
3.1, inheritance,
Generalization relationship: is an inheritance relationship, representing the general and special relationship, which specifies how subclasses specialize all the characteristics and behaviors of their parent classes. For example, a tiger is a kind of animal, which has both tiger characteristics and animal generalities
1) code
Animal:
public class Animal {
private String name;
private Integer age;
public Animal(a) {}public Animal(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age; }}Copy the code
Cats inherited animals:
public class Cat extends Animal {
private String breeds;
public Cat(String name, Integer age, String breeds) {
super(name, age);
this.breeds = breeds;
}
public String getBreeds(a) {
return breeds;
}
public void setBreeds(String breeds) {
this.breeds = breeds;
}
public void selfIntroduction(String name,Integer age,String breeds){
System.out.println("My name is"+name+"It's one."+breeds+"Breed of cat this year."+age+"I'm old."); }}Copy the code
We use a class diagram to represent this relationship.
4) graphic
You have to use the right arrow, or the relationship is completely different.
3.2, implementation,
[Implementation relation] : is a relationship between a class and an interface, indicating that the class is the implementation of all the characteristics and behaviors of the interface.
1) code
Eat and sleep interface, we let the animal class to achieve his two.
public interface Eat {
void eat(a);
}
Copy the code
public interface Sleep {
void sleep(a);
}
Copy the code
public class Animal implements Eat.Sleep{
private String name;
private Integer age;
public Animal(a) {}public Animal(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public void eat(a) {
System.out.println("吃东西");
}
@Override
public void sleep(a) {
System.out.println("Sleep"); }}Copy the code
2) graphic
4. Class diagram of associated relations
Association relation: an ownership relation, which makes one class know the properties and methods of another class; For example, the relationship between teacher and student, husband and wife can be two-way or one-way. A two-way association can have two arrows or no arrows, and a one-way association has one arrow.
Let’s add a place of origin. Every animal will have a place of birth.
We associate the birthplace with animals.
4.1, code,
/ * * *@Author: crush
* @Date: 2021-09-30 19:11 * Version 1.0 * Birthplace */
public class Birthplace {
private String birthplace;
public Birthplace(String birthplace) {
this.birthplace = birthplace;
}
public String getBirthplace(a) {
return birthplace;
}
public void setBirthplace(String birthplace) {
this.birthplace = birthplace; }}Copy the code
2. Relating to animals:
public class Animal implements Eat.Sleep{
private String name;
private Integer age;
private Birthplace birthplace;
public Animal(a) {}public Animal(String name, Integer age, Birthplace birthplace) {
this.name = name;
this.age = age;
this.birthplace = birthplace;
}
public Birthplace getBirthplace(a) {
return birthplace;
}
public void setBirthplace(Birthplace birthplace) {
this.birthplace = birthplace;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge(a) {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public void eat(a) {
System.out.println("吃东西");
}
@Override
public void sleep(a) {
System.out.println("Sleep"); }}Copy the code
Add output to the self-introduction method.
public class Cat extends Animal {
private String breeds;
public Cat(String name, Integer age,Birthplace birthplace, String breeds) {
super(name, age,birthplace);
this.breeds = breeds;
}
public String getBreeds(a) {
return breeds;
}
public void setBreeds(String breeds) {
this.breeds = breeds;
}
public void selfIntroduction(String name,Integer age,String breeds,Birthplace birthplace){
System.out.println("My name is"+name+"It's one."+breeds+"Breed of cat this year."+age+"Old, born."+birthplace.getBirthplace()); }}Copy the code
4.2 diagrams,
Class diagrams for aggregation and composition relations
5.1, aggregation,
Aggregation is the relationship between the whole and the parts, and the parts can exist independently of the whole. If the car and tire are whole and part of the relationship, the tire can still exist without the car. Aggregation relation is a kind of correlation relation, which is strong correlation relation. Association and aggregation are syntactically indistinguishable and must be examined for specific logical relationships.
So let’s talk about my example, and then we’ll write the code.
Every car will have four tires and an engine. The tires can stand alone without the car, and so can the engine.
1) code
public class Wheel {
private String type;
public Wheel(String type) {
this.type = type;
}
public String getType(a) {
return type;
}
public void setType(String type) {
this.type = type;
}
public void move(a){
System.out.println("Roll!!"); }}Copy the code
public class Engine {
private String type;
public Engine(String type) {
this.type = type;
}
public String getType(a) {
return type;
}
public void setType(String type) {
this.type = type;
}
public void start(a){
System.out.println("Start the engine!!"); }}Copy the code
public class Car {
private Wheel wheel;
private Engine engine;
public Car(Wheel wheel, Engine engine) {
this.wheel = wheel;
this.engine = engine;
}
public Wheel getWheel(a) {
return wheel;
}
public void setWheel(Wheel wheel) {
this.wheel = wheel;
}
public Engine getEngine(a) {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public void go(a){
System.out.println("Drive out the waves; Ah. ""); }}Copy the code
How to represent le in a class diagram, and then go to 👇
2) graphic
Combination of 5.2,
Composition: the relationship between the whole and the parts, but the parts cannot exist without the whole. If companies and departments are integral and partial, there would be no departments without companies. And if a person is composed of head and body, without a person, the head and body will not exist. Composition relation is a kind of association relation, which is stronger than aggregation relation. It requires the object representing the whole in ordinary aggregation relation to be responsible for the life cycle of the object representing the part.
1) code
public class Body {
private double size;
public Body(double size) {
this.size = size;
}
public double getSize(a) {
return size;
}
public void setSize(double size) {
this.size = size; }}Copy the code
public class Engine {
private String type;
public Engine(String type) {
this.type = type;
}
public String getType(a) {
return type;
}
public void setType(String type) {
this.type = type;
}
public void start(a){
System.out.println("Start the engine!!"); }}Copy the code
public class Person2 {
private String name;
private Head head;
private Body body;
public Person2(String name, Head head, Body body) {
this.name = name;
this.head = head;
this.body = body;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public Head getHead(a) {
return head;
}
public void setHead(Head head) {
this.head = head;
}
public Body getBody(a) {
return body;
}
public void setBody(Body body) {
this.body = body;
}
public void say(a){
System.out.println("I can talk."); }}Copy the code
2) graphic
Class diagram of dependencies
Dependency: A relationship is a usage relationship. It is the least coupled relationship between objects, and it is a temporary relationship. 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. For example, in the diagram of the relationship between people and mobile phones, people make calls through the voice transmission method of mobile phones.
1, code,
public class Phone {
public void callUp(a){
System.out.println("Talking to someone"); }}Copy the code
public class Person3 {
private String name;
public Person3(String name) {
this.name = name;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public void callUp(Phone phone){
System.out.println("Using a cell phone to make a call.");
phone.callUp();
}
// The following way is also a dependency
// public void callUp(){
// Phone phone=new Phone();
// system.out.println (" Use mobile phone to call ");
// phone.callUp();
/ /}
}
Copy the code
2, graphic
Seven, the strength of class relationship
Strength and weakness: Generalization = Implementation > Composition > Aggregation > Association > Dependency
And when we talk about reducing coupling, we also talk about reducing relationships between classes.
8. Talk to yourself
That’s all for today’s article.
Hello, THIS is blogger Ning Zaichun: homepage
If you encounter doubts in the article, please leave a message or private letter, or add the homepage contact information, will reply as soon as possible.
If you find any problems in the article, please correct them. Thank you very much.
If you think it will help you, please click “like” before leaving!
Welcome everyone to discuss in the discussion forum, increase your luck, rub a rub against the nuggets official sent around oh!!
The more you comment, the more likely you are to win an award!!
Details 👉 Diggin project