From the perspective understand UML diagrams IDEA

Foreword

We have now learned seven design principles of design patterns. Here it was supposed to go directly to specific design patterns series of articles.

But before we learn what design patterns we need to look at uml diagram. Because the subsequent design patterns article nothing else should be using a lot of places to uml diagram. If you have not read even uml diagram, then learn to be sure there will be some difficulty.

So, on this section as a nexus chapter, let's take a look uml drawing. (Of course, if you already would UML diagram, you can skip this section)

Introduction

By convention, we let you know what a UML diagram:

There are many types of UML, what's the use case diagram, static structure diagram, the dynamic behavior diagrams.

As a java program apes, we are most concerned about or static structure diagram class diagram you can, so you only need to remember these words on it: UML diagram is the relationship between the various classes, interfaces used to describe .

Below we include the future of UML class diagrams are used IntelliJ IDEA comes with tools to show the class diagram, each software drawing style will be a little different, but the core of things will not change.

OK, I get down

Relations between classes

The relationship between class and class we can be divided into the following:

Because most of the time we are using the idea comes with tools to generate the Uml Support uml diagram (shortcut: ctrl + alt + shift + u). Although the general class diagram is a line drawing of the same, but the relationship between the polymerization and the combination relation IDEA UML diagrams generated a little bit different, we then look down.

rely

As long as the other side is used in the class, then there dependencies between them.

Dependencies include other relations 5. It is the largest special relationship.

  1. Used in the other class
  2. If the property is a member of the class
  3. If the return type
  4. Method of parameter type is received
  5. The method used to
//手机类
public class CellPhone {
    
    //手机可以玩游戏
    public void playGames(){
        System.out.println("play games");
    }
}


//普通人
public class Person {

    //买手机
    public CellPhone buyCellPhone(){
        return new CellPhone();
    }
    
    //买个手机玩游戏
    public void play(){
        CellPhone cellPhone = buyCellPhone();
        cellPhone.playGames();
    }
}

Generalization

Generalization is actually inherited, he is a special case of dependencies (proper subset).

  1. Generalization is actually inheritance
  2. If the A class inherits class B, we say that there is a generalization relationship A and B
//Person代表人
public class Person {

}

//Man代表男人
public class Man extends  Person{
    
}

achieve

A relationship is actually implemented B class implements the interface, he is a special case of dependencies (subset).

//Dao接口
public interface Dao {

}
//Dao实现
public class DaoImpl implements  Dao {
    
}

Association, aggregation, composition

Why these three together it?

Because the same performance in the three codes, but some differences on semantics.

1 related:

The link between class and class relationships with multiple resistance, such as:

“1”(表示有且仅有一个)

“0...”(表示0个或者多个)

“0,1”(表示0个或者一个)

“n...m”(表示n到 m个都可以)

“m...*”(表示至少m个)

2 Polymerization:

部分可以离开整体单独存在,举一个例子。学校类中有一个学生类。当我们创建一个学校类的时候,可能因为刚创办,还没有学生。所以学生类是可以不存在的。不影响学校类的创建。他是依赖关系的特例(真子集)。

3 组合:

整体和部分同生共死,部分脱离整体会变得毫无意义,强调同生共死的一致的生命周期。

例如学生类中的身份证证类。每个学生肯定都会有身份证。在学生类被实例化成功以后,身份证类也被实例化成功。学生类是不能脱离身份证类单独存在的。他是依赖关系的特例(真子集)。


//学校
public class School {
    public List<Student> studnets;
}


//学生
public class Student {
   private IdentityCard identityCard = new IdentityCard(); //组合关系,创建student的时候也创建了身份证
}

//身份证
public class IdentityCard {
   private String id = UUID.randomUUID().toString();
}

一个学校有很多个学生,一个学生只有一个身份证。所以上面三个类的UML图如下:

首先我们来看实线箭头,箭头方向指的是依赖的方向。School箭头指向Student标注1 * 表示:一个学校有多个学生。

Student的实线箭头指向IdentityCard并且标注的是1 1表示:一个学生只有一个学校和一个身份证。

然后我们看菱形图。正常来说,聚合关系应该是空心的菱形图,组合关系才是实心的菱形图,但是IDEA的集成工具将聚合和依赖关系都以实心菱形图来表示。

按照标准的uml图来说,student和school之间应该是空心的菱形图。IdentityCard与Student才是实线的菱形图。

总结

虽然说idea的画法有点不同,为了他的便利性我们也忍了。就好比大肠的功能虽然很脏,但是为了它的美味我能仍受,并且享受它。

好了,UNL图就到这里了,我们前期铺垫了那么多,从下一节开始终于进入了主题,具体的设计模式系列。

Guess you like

Origin www.cnblogs.com/zhxiansheng/p/11368400.html