六种常用UML关系(代码实例)

来源:互联网 发布:usb编程案例 编辑:程序博客网 时间:2024/06/05 14:31

依赖(Dependence)

  • 表示一个类依赖于另一个类的定义。依赖关系总是单向的。
  • 简单的理解,就是一个类A使用到了另一个类B,而这种使用关系是具有偶然性的、临时性的、非常弱的,但是B类的变化会影响到A。
  • 在代码层面,表现为类B作为参数被类A在某个method方法中使用。具体体现为局部变量、方法中的参数、静态方法的调用等。

代码实例:

public class Book {    private String name;    public Book() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
public class Food {    private String name;    public Food() {    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
public class People {    private String name;    public People(String name) {        this.name = name;    }    //Dependency    public void eat(Food food){        System.out.println("eat " + food.getName());    }    //Dependency    public void read(Book book){        System.out.println("read " + book.getName());    }}
public class Main {    public static void main(String[] args) {        People people = new People("White");        Food food = new Food();        food.setName("seafood");        Book book = new Book();        book.setName("cs");        people.eat(food);        people.read(book);    }}

以上代码中People类依赖Food类和Book类,这种依赖关系表现为Food类和Book类作为People类中方法的参数,局部变量和静态方法调用的情况也比较简单,此处省略。

关联(Association)

  • 表示类与类之间的联接, 它使一个类知道另一个类的属性和方法。
  • 关联可以使用单箭头表示单向关联, 使用双箭头或不使用箭头表示双向关联, 不建议使用双向关联.。关联有两个端点, 在每个端点可以有一个基数, 表示这个关联的类可以有几个实例。
  • 单向关联表现为类A当中使用了类B,其中类B是作为类A的成员变量。双向关联表现为类A当中使用了类B作为成员变量,同时类B中也使用了类A作为成员变量。

代码实例:

public class Father {    //Association    private Son son;    public Father() {    }    public void giveGift(){        System.out.println("father gives a gift to son");    }    public Son getSon() {        return son;    }    public void setSon(Son son) {        this.son = son;    }}
public class Son {    //Association    private Father father;    public Son() {    }    public void getGift(){        System.out.println("son gets a gift from father");    }    public Father getFather() {        return father;    }    public void setFather(Father father) {        this.father = father;    }}
public class Main {    public static void main(String[] args) {        Father father = new Father();        Son son = new Son();        father.setSon(son);        son.setFather(father);        father.giveGift();        son.getGift();    }}

以上代码中Father类和Son类相互关联,各自使用对方作为自己的成员变量。

聚合(Aggregation)

  • 聚合是关联关系的一种特例,耦合性比关联关系更强。
  • 聚合表示的是整体与个体之间的关系,即has-a的关系。
  • 整体与个体之间是可以分离的,他们各自有自己的生命周期。
  • 它和关联关系的区别在于,关联关系的两个类是同等级别的,而聚合关系中有整体和个体的区分。

代码实例:

public class House {    private String address;    public House(String address) {        this.address = address;    }    public void getHouseAddress(){        System.out.println(this.address);    }}
public class Car {    private String brand;    public Car(String brand) {        this.brand = brand;    }    public void getCarBrand(){        System.out.println(this.brand);    }}
public class People {    private House house;    private Car car;    public People() {    }    public void showAssets(){        house.getHouseAddress();        car.getCarBrand();    }    public House getHouse() {        return house;    }    public void setHouse(House house) {        this.house = house;    }    public Car getCar() {        return car;    }    public void setCar(Car car) {        this.car = car;    }}
public class Main {    public static void main(String[] args) {        House house = new House("Central Park");        Car car = new Car("Ford");        People p = new People();        p.setCar(car);        p.setHouse(house);        p.showAssets();    }}

以上代码中People类和House类、Car类是一个整体与个体的关系,它们都有各自的生命周期,是一种聚合关系。

组合(Composition)

  • 组合关系也是关联关系的一种特例,耦合性比聚合关系更强,它体现的是整体与部分的关系,表现为contains-a。整体和部分是不可分割的,要么同生,要么共死。

代码实例:

public class Body {    public Body() {    }    public void showBody(){        System.out.println("body");    }}
public class Soul {    public Soul() {    }    public void showSoul(){        System.out.println("soul");    }}
public class People {    private Body body;    private Soul soul;    public People(Body body,Soul soul) {        this.body = body;        this.soul = soul;    }    public void print(){        body.showBody();        soul.showSoul();    }}
public class Main {    public static void main(String[] args) {        People people = new People(new Body(),new Soul());        people.print();    }}

以上代码中,People类和Body类、Soul类是整体与部分的关系,它们同生同死,不能分离单独存在。比如,这边Body和Soul都是在People的构造函数中实例化的,表示同生,且不能更换。而在前面的聚合关系中,People的House和Car是可以随时更换的。

继承/泛化(Generalization)

  • 表示类与类(或者接口与接口)之间的父子关系。

代码实例:

public class Animal {    public Animal() {    }    public void makSound(){        System.out.println("Animal make sound!");    }}
public class Human extends Animal{    public Human() {    }    @Override    public void makSound() {        System.out.println("Human make sound!");    }}
public class Main {    public static void main(String[] args) {        Animal peter = new Human();        peter.makSound();    }}

实现(Implementation)

  • 表示一个类实现一个或多个接口的方法,接口定义好操作的集合,由实现类去完成接口的具体操作。

代码实例:

public interface Animal {    public void eat();    public void move();}
public class Human implements Animal {    public Human() {    }    @Override    public void eat() {        System.out.println("Human eat...");    }    @Override    public void move() {        System.out.println("Human move...");    }}
public class Main {    public static void main(String[] args) {        Animal mason = new Human();        mason.eat();mason.move();    }}

结束。

0 0
原创粉丝点击